【问题标题】:Object not holding instance when accessed through method通过方法访问时对象不持有实例
【发布时间】:2014-10-06 09:19:34
【问题描述】:

我创建了一个模型类“RestaurentList”,它使用来自 json 文件的数据填充两个集合。 在我的 ViewModel 中将对象实例化为类后,我将集合数据绑定到 ItemsControl。

上面的一切都很好,但是当我从我的 ViewModel 中的对象调用方法 populatePartialList 时,它不包含我实例化对象时的任何数据。 这意味着,当我的方法尝试重新填充 PartialList 时,它不能,因为它没有从 FullList 中找到数据。

编辑:我遗漏了一些代码,正如您在注释标签中看到的那样。 我只是想让你了解我是如何做到这一点的。

我的问题基本上是,为什么当我调用方法 populatePartialList 时对象不包含任何数据。

我猜这与我将 List 数据绑定到 ItemsControl 的事实有关,因此无法再访问它?那这种情况应该怎么办呢?我正在尝试制作一个非常简单的分页

对以上内容进行编辑;我尝试删除我的绑定,但仍然无法访问数据。

型号:

public class RestaurentList
    {
        private ObservableCollection<Restaurent> _fullList = new ObservableCollection<Restaurent>();

        private ObservableCollection<Restaurent> _partialList = new ObservableCollection<Restaurent>();

        public ObservableCollection<Restaurent> FullList
        {
            get { return _fullList; }
        }

        public ObservableCollection<Restaurent> PartialList
        {
            get { return _partialList; }
        }

        public RestaurentList()
        {
            populateList();
        }

        public void populatePartialList(int fromValue = 1)
        {
            int collectionAmount = _fullList.Count;
            int itemsToShow = 2;
            fromValue = (fromValue > collectionAmount ? 1 : fromValue);

            foreach (Restaurent currentRestaurent in _fullList)
            {
                int currentId = Convert.ToInt32(currentRestaurent.UniqueId);
                if (currentId == fromValue || (currentId > fromValue && currentId <= (fromValue + itemsToShow)-1))
                {
                    _partialList.Add(currentRestaurent);
                }
            }
        } 

        private async void populateList()
        {
            // Get json data

            foreach (JsonValue restaurentValue in jsonArray)
            {
                // populate full list

                foreach (JsonValue menuValue in restaurentObject["Menu"].GetArray())
                {
                    // populate full list
                }
                this._fullList.Add(restaurent);
            }
            populatePartialList();
        }

        public override string ToString()
        {
            // Code
        }
    }

查看模型:

class ViewModelDefault : INotifyPropertyChanged
    {


        private RestaurentList _list;

        public ObservableCollection<Restaurent> List
        {
            get { return _list.PartialList; }
        }


        public ViewModelDefault()
        {  
            _list = new RestaurentList();
            _list.populatePartialList(2); // This is where i don't see the data from RestaurentList
        }

        #region Notify
    }

为乔恩编辑:

public RestaurentList()
        {
            PopulatePartialList();
        }

        public async void PopulatePartialList(int fromValue = 1)
        {
            await PopulateList();
            int collectionAmount =  _fullList.Count;
            int itemsToShow = 2;
            fromValue = (fromValue > collectionAmount ? 1 : fromValue);

            foreach (Restaurent currentRestaurent in _fullList)
            {
                int currentId = Convert.ToInt32(currentRestaurent.UniqueId);
                if (currentId == fromValue || (currentId > fromValue && currentId <= (fromValue + itemsToShow)-1))
                {
                    _partialList.Add(currentRestaurent);
                }
            }
        } 

        private async Task PopulateList()
        {
}

【问题讨论】:

  • 附带说明,餐厅以ant 结尾,而不是ent - 现在是修改名称的好时机。我还强烈建议您遵循方法的 .NET 命名约定,即 PopulatePartialList 等。

标签: c# .net xaml data-binding observablecollection


【解决方案1】:

在调用populatePartialList之前查看代码行:

_list = new RestaurentList();

您已创建RestaurentList 的新实例。这将调用populateList(),但等待它完成。假设您对populateList 的实际实现包含await 操作,这意味着您对populatePartialList(2) 的调用几乎肯定会在数据准备好之前发生。

您需要考虑异步如何在这里工作,以及您希望它如何工作。请注意,虽然您不能有一个异步构造函数,但您可以有一个异步静态方法......这对于ViewModelDefaultRestaurentList 来说可能是一个更好的主意。

【讨论】:

  • 这是有道理的。我只是在学习异步,如果这可能太明显了,我很抱歉。
  • @Mathias:异步中很少有明显的;)说实话,预计需要一段时间才能理解......
  • 呵呵。不过感谢您的提示。我必须确保我明白在这里做什么。如果我让我的 populatePartialList 异步工作,我如何让它等待首先填充的 FullList?
  • 我对帖子进行了编辑,将 populateList 制作成一个任务,然后我在 populatePartialList 中使用 await 执行该任务。它也没有按照我想要的方式运行。我还没有完全分析。
  • @Mathias:我认为在另一个问题中提出这个问题是值得的——但从根本上说,问题在于PopulatePartialList 是一个async void 方法,而不是返回一个等待等待的任务。 . 并且您仍然从构造函数中调用它,无论如何它都无法等待它。
猜你喜欢
  • 1970-01-01
  • 2014-01-14
  • 1970-01-01
  • 2018-02-24
  • 2021-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多