MVVMLight和数据序列化 [英] MVVMLight and Data Serialisation

查看:185
本文介绍了MVVMLight和数据序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在针对Windows 8 Store应用程序使用MVVMLight.观看一些视频后,我已经掌握了基本知识.但是我遇到了一个问题.我的每个基本模型类都继承自MVVMLight中的ObservableObject.

I'm getting started with MVVMLight for a Windows 8 Store app. I've got the basics working after viewing some videos. however I have run into an issue. Each of my base model classes inherit from ObservableObject in MVVMLight.

这工作正常,但我现在想将数据加载并保存到XML.因此,我用之前在非MVVM实现中使用的DataContract属性标记了它们.但是,这现在在序列化时会产生一个错误,因为它说我继承的类也必须使用此属性进行标记.

This was working fine but I now want to load and save data to XML. So I marked them with DataContract attribute which I'd used previously in a non MVVM implementation. However this now creates an error when serialising as it says my inherited classes must also be marked with this attribute.

由于ObservableCollection是在dll中编译的,我该如何管理它?我是否需要创建一组与我的"ViewModel"样式匹配的基本(POCO样式)类,并处理它们之间的映射.还是有更好的方法?

As ObservableCollection is compiled in the dll how should I manage this? Will I have to create a set of basic (POCO style) classes which match up with my "ViewModel" style ones and handle the mapping between these. Or is there a better way?

推荐答案

您不想序列化视图模型,而是想要序列化它们的当前状态,以便它们在重新启动应用程序时可以自行重建.

You don't want to serialize your viewmodels, you want to serialize their current state so that they can rebuild themselves when the app is restarted.

所以,像这样:

public class ViewModelFoo
{
    public ViewModelFoo(ISerializationService serializationService)
    {
         _serializationService=serializationService;
         LoadDefaultData();
    }

    private void LoadDefaultData()
    {
        //Do all your loading of static data here
         FooItems=GetFooItems();
         if(_serializationService.ContainsSerializedState)
         {
              LoadSerializedState();
         }
    }

    public Observable<Foo> FooItems{get;set;}

    public Foo SelectedFooItem
    {
        get{return _fooItem;}
        set{_fooItem=value;
            RaisePropertyChanged("SelectedFooItem"); 
            _serializationService.SelecetedFooItem=value;
           }
    }

    private void LoadSerializedData()
    {
        SelectedFooItem=_serializationService.SelectedFooItem;
        ReloadData();
    }

    private void ReloadData()
    {
        //load whatever data you need.  You've now got your app back into the state it was when it was serialized;
    }
}

基本上,每次更改屏幕上的内容时,我们都会更新状态对象.当我们暂停应用程序(App类中的事件)时,我们将保存状态对象.

Basically, we're updating the state object every time we change something on the screen. We would save the state object when we suspended the app (the event in the App class).

通过这样做,我们能够存储视图模型的状态,而不是视图模型本身.因为我们控制序列化服务,所以可以使用我们要使用的任何序列化方法来保存数据.

By doing this we're able to store the state of our viewmodel rather than the viewmodel itself. Because we control the serialization service we can save our data using whatever serialization method we want to use.

这篇关于MVVMLight和数据序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆