我想知道我的gridView是否为空 - 其中没有任何项目 . 我试图做以下事情:
public sealed partial class Profile : Page
{
Boolean isGridViewEmpty = true;
}
这是显示网格视图的函数,我试图让它也确定gridview是否为空
//gets the animals of the specific chosen user's data tabe
public async void getAnimalsData(int ownerId)
{
int count = 0;
regitration.getAnimalsOfUserTableResponseGetAnimalsOfUserTableResult r = await cal.getAnimalsOfUserTableAsync(ownerId);
List<Animal> theAnimalList = new List<Animal>();
Animal a = null;
XmlReader xr = r.Any1.CreateReader();
XmlDocument document = new XmlDocument();
document.Load(xr);
XmlNodeList theXmlList = document.GetElementsByTagName("Table");
foreach (XmlElement item in theXmlList)
{
a = new Animal();
foreach (XmlNode node in item.ChildNodes)
{
switch (node.Name)
{
case "animalId": a.AnimalId = int.Parse(node.InnerText); count++; break;
case "ownerId": a.OwnerId = int.Parse(node.InnerText); count++; break;
case "animalName": a.Animalname = node.InnerText; count++; break;
case "fur": a.Fur = node.InnerText; count++; break;
case "level": a.Level = int.Parse(node.InnerText); count++; break;
case "money": a.Money = int.Parse(node.InnerText); count++; break;
}
}
theAnimalList.Add(a);
}
grid2.ItemsSource = theAnimalList;
if (count == 0)
{
isGridViewEmpty = true;
}
else
{
isGridViewEmpty = false;
}
}
经过调试,我可以看到它没有显示错误信息 . 它只是在最后一个花括号后卡住了 . 我不知道我做错了什么,计数似乎工作正常,在调试时它也告诉我 isGridViewEmpty
确实设置为true,但每当我来实现该功能并且我检查 isGridViewEmpty
是否为真时,它不会不行 . 另外,正如我之前提到的,调试器卡在函数 getAnimalsData
中
1 回答
函数
getAnimalsData
是async
. 所以它实际上是有效的,因为它发生在后台而不是我按下它的顺序,所以每当我有条件相关的功能时,它看起来没用 . 所以我把它变成了Task而不是void
并在函数前放了await
.有关更多详细信息,请参阅Asynchronous programming with async and await (C#) .