开发者

Retrieving data from isolated storage into viewmodel

I am having a serious mental block here and am in departate need for some help. I am using the Windows Phone Databound Applicati开发者_开发百科on 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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜