简体   繁体   English

检查可观察的集合是否为空! C#

[英]check to see if an observable collection is empty ! c#

want to check if there are ant Items in an observable collection named PlayerProfile 要检查在名为PlayerProfile的可观察集合中是否有蚂蚁物品

if ((App.ViewModel.PlayerProfile.Count != 0))
{
  // remove stored PlayerProfile
}

Anyideas ?? 有任何想法吗 ??

gettign a null error (even though there are gettign空错误(即使有

App.ViewModel.PlayerProfile is an oberservable collection containg players App.ViewModel.PlayerProfile是一个可观察的集合,包含播放器

i just want to check to see if its empty and if not delete or clear 我只想检查一下是否为空,如果不删除或清除

If you're getting a null error, there are three possibilities: 如果出现空错误,则有以下三种可能性:

  1. App is null (very unlikely) App为空(极不可能)
  2. App.ViewModel could be null. App.ViewModel可以为null。
  3. The PlayerProfile property on ViewModel may be null. ViewModel上的PlayerProfile属性可以为null。

I suggest making sure that PlayerProfile is being constructed in all constructors of your ViewModel, and that your ViewModel has been constructed at this point, as those are the most likely culprits. 我建议确保在ViewModel的所有构造函数中都构造PlayerProfile ,并在这一点上构造ViewModel,因为这是最有可能的罪魁祸首。 Most likely, this routine is being called prior to your initialization methods, which is why these are unset at this point. 最有可能在初始化方法之前调用此例程,这就是为什么此时未设置这些例程的原因。

The Count property will return the number of elements in the collection. Count属性将返回集合中的元素数。

However, if you're getting a null error, that means something is null. 但是,如果收到null错误,则表示某些内容为null。

AppViewModelPlayerProfilenull

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM