将数据从独立存储检索到视图模型中

发布于 2024-11-18 06:35:38 字数 1228 浏览 4 评论 0原文

我在这里有严重的精神障碍,非常需要一些帮助。我使用 Windows Phone 数据绑定应用程序作为我的项目的起点,并且对模型、视图模型和视图的交互方式相当满意。

我的问题是示例中使用的数据是在运行时创建并添加到 ObservableCollection 对象中的。我的理解是,创建新的 ItemViewModel 对象以及随后将它们添加到 ObservableCollection 会触发与 NotifyPropertyChanged 事件等效的事件,从而确保视图上的绑定全部刷新。如果我的解释不正确,那么我欢迎任何想法。

现在,数据的运行时生成在大多数实际示例中是无用的,因此我需要将数据序列化到隔离存储。我已经完成了这一点,并且可以成功保存和加载我的 ObservableCollection 对象。

当我从isolatedStorage加载数据,然后将返回的ObservableCollection分配给视图模型中的Items对象时,出现了我的问题。

    public void LoadData()
    {
        App.Measurements = Serialization.Read<measurements>(App.MEASUREMENTS);
        this.IsDataLoaded = true;
    }

App.MEASUREMENTS 只是一个全局变量,其中包含要在isolatedStorage 中使用的文件名。

上面的代码检索数据并将其分配给 Items ObservableCollection 但 UI 未更新。如果我将上面的代码替换为:

    public void LoadData()
    {
        foreach (measurement m in App.Measurements.WeightMeasurements)
        {
            this.Items.Add(m);
        }
        this.IsDataLoaded = true;
    }

并迭代检索到的集合并将新的 ItemViewModel 集合添加到 Items 集合中,则 UI 上的所有内容都会正确更新。

我的 xaml 具有正确的绑定,并且 DataContext 也设置正确。

我尝试了多种方法来解决这个问题,但我不敢相信唯一的方法是迭代整个加载的集合,将其添加到 Items 集合中,只有一些事件才会触发!

任何想法都非常受欢迎。

杰森.

I am having a serious mental block here and am in departate need for some help. I am using the Windows Phone Databound Application as the starting point of my project and am reasonably happy with how the model,viewmodel and view interact.

My issue is that the data used in the example is created at run time and added to the ObservableCollection object. My understanding is that the creation of the new ItemViewModel objects and the subsequent adding of them to the ObservableCollection fires the equavalent to the NotifyPropertyChanged events which ensure that the bindings on the view all refresh. If my explanation is incorrect then I would welcome any thoughts.

Now, the runtime generation of data is useless in most real-world examples and as such I need to serialize the data to isolated storage. This I have done and can successfully save and load my ObservableCollection object.

My problem arises when I load the data from IsolatedStorage and then assign the returned ObservableCollection to the Items object in the view model.

    public void LoadData()
    {
        App.Measurements = Serialization.Read<measurements>(App.MEASUREMENTS);
        this.IsDataLoaded = true;
    }

App.MEASUREMENTS in simply a global variable that contains the file name to be used in IsolatedStorage.

The above code retrieves the data and assigns it to the Items ObservableCollection but the UI is not updated. If I replace the code above with:

    public void LoadData()
    {
        foreach (measurement m in App.Measurements.WeightMeasurements)
        {
            this.Items.Add(m);
        }
        this.IsDataLoaded = true;
    }

and iterate throught the retrieved collection and add a new ItemViewModel collection to the Items collection then everything updates on the UI correctly.

My xaml has the correct bindings and the DataContext is set correctly too.

I have tried numersou ways trying to solve this but I can't believe that the only way is to iterate through the entire loaded collection adding it to the Items collections just some the event will fire!

Any thoughts most welcomed.

Jason.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回梦 2024-11-25 06:35:38

您是否在 Items 上实现 INotifyPropertyChanged?

作为 ObservableCollection,当集合中的单个项目发生更改时,它将处理对它们的 INPC 调用,但您仍然需要为属性本身处理此问题。

Are you implementing INotifyPropertyChanged on Items?

As an ObservableCollection it will handle calling INPC on individual items within the collection when they're changed but you still need to handle this for the property itself.

无边思念无边月 2024-11-25 06:35:38

最终还是追到了底部。这与 ObservableCollection 的定义有关。开箱即用,它看起来像这样

    public ObservableCollection<ItemViewModel> Items { get; private set; }

但是,仅当属性定义如下时才会触发 NotifyPropertyChanged:

    private ObservableCollection<ItemViewModel> _items;
    public ObservableCollection<ItemViewModel> Items
    {
        get
        {
            return _items;
        }
        private set
        {
            if(_items != value)
                _items = value;
            NotifyPropertyChanged("Items");
        }
    }

Items 实现了 INotifyPropertyChanged 但在分配 Items 对象时从未调用它。

现在一切都按预期工作,无需迭代加载的集合并将其添加到 Items ObservableCollection 中。

Got to the bottom of it in the end. It was to do with the definition of the ObservableCollection. OUt of the box it looks like this

    public ObservableCollection<ItemViewModel> Items { get; private set; }

However, the NotifyPropertyChanged only gets fired when the property is defined as such:

    private ObservableCollection<ItemViewModel> _items;
    public ObservableCollection<ItemViewModel> Items
    {
        get
        {
            return _items;
        }
        private set
        {
            if(_items != value)
                _items = value;
            NotifyPropertyChanged("Items");
        }
    }

Items implemented INotifyPropertyChanged but it was never getting called when the Items object was being assigned to.

Everything works just as expected now and there is no need to iterate through the loaded collection and add it to the Items ObservableCollection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文