Serialization MVVM灯光和WP7 ViewModel墓碑显示不是';行不通

Serialization MVVM灯光和WP7 ViewModel墓碑显示不是';行不通,serialization,windows-phone-7,viewmodel,mvvm-light,tombstoning,Serialization,Windows Phone 7,Viewmodel,Mvvm Light,Tombstoning,我试着按照中的步骤进行操作,但无法让它对我起作用。毫无疑问,我做错了什么。在我的ViewModel中: private string _foobar ="init"; public string testStr { get { return _foobar; } set { _foobar = value; } } 在我的页面中: <TextBox x:Name="tBoxTest" Text="{Bin

我试着按照中的步骤进行操作,但无法让它对我起作用。毫无疑问,我做错了什么。在我的ViewModel中:

private string _foobar ="init";

public string testStr
{
    get
    {
        return _foobar;
    }

    set
    {
        _foobar = value;
    }
}
在我的页面中:

<TextBox x:Name="tBoxTest" Text="{Binding testStr, Mode=TwoWay}" />


当应用程序运行时,更改tBoxTest集合中的值\u foobar很好,但是尝试序列化它,就好像它忘记了实例???任何帮助都将不胜感激。

从您发布的代码中,没有即时的答案

我的建议是:

  • 如果您完全从那篇文章中复制了代码,那么可以向空的catch处理程序添加一些东西(messagebox?)-`catch(Exception){}

  •     set
        {
             RaisePropertyChanged("TestStr");
            _foobar = value;
        }
    }
    
  • 使用调试器在LoadModel和SaveToIsolatedStorage方法中放置断点

  • 使用这些断点逐步加载和保存代码-代码是否正确加载和保存


老实说,对于这样的问题,自己做一点调查要比在这里提问好得多(依我看!)

从你发布的代码中没有即时的答案

我的建议是:

  • 如果您完全从那篇文章中复制了代码,那么可以向空的catch处理程序添加一些东西(messagebox?)-`catch(Exception){}

  •     set
        {
             RaisePropertyChanged("TestStr");
            _foobar = value;
        }
    }
    
  • 使用调试器在LoadModel和SaveToIsolatedStorage方法中放置断点

  • 使用这些断点逐步加载和保存代码-代码是否正确加载和保存


老实说,对于这样的问题,自己做一点调查要比在这里问问题好得多(IMO!)

通过执行以下操作,我可以让墓碑开始工作,同时让我所有的ViewModels都可以看到一个对象:

在模型类中,我添加了:

private static Model1 _instance;
public static Model1 Instance
{
    get { return _instance; }
    set { _instance = value; }
}

public static void CreateNew()
{
    if (_instance == null)
    {
        _instance = new Model1();

        _instance.FirstString = "init";
    }
}
然后在ApplicationExtensions.cs中,我添加了:

  public static void SaveToIsolatedStorage(this Application app, Model1 model)
    {
        var dataFileName = GetIsFile((model.GetType()));
        using (var userAppStore =
                 IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (userAppStore.FileExists(dataFileName))
            {
                userAppStore.DeleteFile(dataFileName);
            }
            using (var iss = userAppStore.CreateFile(dataFileName))
            {
                SilverlightSerializer.Serialize(model, iss);
            }
        }
    }
在App.xaml.cs中,我将LoadModel()更改为:

我的意思是Model1对象正在被序列化,墓碑正在工作——至少我得到了我认为我想要的东西。我通过在应用程序之间导航、手机设置、关闭和打开手机、锁定手机以及从另一部手机在应用程序中呼叫手机,对它进行了很多测试。反序列化时的性能非常好。我可以和vars一起工作

话虽如此,Van Schaik先生回答了一个协助请求:“如果您是从MVVMLight ViewModelBase中进行子类化,那么您应该从setter调用RaisePropertyChanged,如下所示:

私有字符串\u foobar=“init”

公共字符串TestStr { 得到 { 返回\u foobar; }

    set
    {
         RaisePropertyChanged("TestStr");
        _foobar = value;
    }
}
RaisePropertyChanged通知任何Listings视图(即绑定到它的文本框)某个属性已更改,并且应更新其内容。这是一个关键机制。”

因此,我将使用我最初尝试的内容,但添加RaisePropertyChanged以查看它的作用

更新

虽然我在MainViewModel.cs文件中实现了RaisedPropertyChanged(使用代码段mvvminpc),但这对序列化ViewModel中创建的任何内容仍然没有任何影响(与其他方面一样好)。我可能仍然做了一些错误的事情,但也可能是因为视图模型继承自受保护的类()。I(非常不情愿地)尝试将该类从protected更改为public并重新编译,但在我的情况下没有任何帮助,我不喜欢这样创建引用库。无论如何,我现在正在努力在App.xaml.cs中创建Model1实例。似乎可以工作。在这期间,我修改了Van Schaik的一个方法以接受任何类型的对象:

public static void SaveToIsolatedStorage<T>(this Application app, T obj)
where T : class
{
    var dataFileName = GetIsFile(typeof(T));
    using (var userAppStore =
                IsolatedStorageFile.GetUserStoreForApplication())
    {
        if (userAppStore.FileExists(dataFileName))
        {
            userAppStore.DeleteFile(dataFileName);
        }
        using (var iss = userAppStore.CreateFile(dataFileName))
        {
            SilverlightSerializer.Serialize(obj, iss);
        }
    }
}
public static void SaveToIsolatedStorage(此应用程序应用程序,T obj)
T:在哪里上课
{
var dataFileName=GetIsFile(typeof(T));
使用(var userAppStore)=
IsolatedStorageFile.GetUserStoreForApplication())
{
if(userAppStore.FileExists(dataFileName))
{
userAppStore.DeleteFile(数据文件名);
}
使用(var iss=userAppStore.CreateFile(dataFileName))
{
SilverlightSerializer.Serialize(obj,iss);
}
}
}

通过执行以下操作,我可以让墓碑图正常工作,同时让我的所有ViewModels都可以看到一个对象:

在模型类中,我添加了:

private static Model1 _instance;
public static Model1 Instance
{
    get { return _instance; }
    set { _instance = value; }
}

public static void CreateNew()
{
    if (_instance == null)
    {
        _instance = new Model1();

        _instance.FirstString = "init";
    }
}
然后在ApplicationExtensions.cs中,我添加了:

  public static void SaveToIsolatedStorage(this Application app, Model1 model)
    {
        var dataFileName = GetIsFile((model.GetType()));
        using (var userAppStore =
                 IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (userAppStore.FileExists(dataFileName))
            {
                userAppStore.DeleteFile(dataFileName);
            }
            using (var iss = userAppStore.CreateFile(dataFileName))
            {
                SilverlightSerializer.Serialize(model, iss);
            }
        }
    }
在App.xaml.cs中,我将LoadModel()更改为:

我的意思是Model1对象正在序列化,逻辑删除正在工作-至少我得到了我认为我想要的东西。我已经通过在应用程序、手机设置、手机开关、锁定和从另一部手机在应用程序中调用进行了大量测试。反序列化时的性能非常好。而且我与vars的合作

话虽如此,Van Schaik先生回答了一个协助请求:“如果您是从MVVMLight ViewModelBase中进行子类化,那么您应该从setter调用RaisePropertyChanged,如下所示:

私有字符串\u foobar=“init”

公共字符串TestStr { 得到 { 返回\u foobar; }

    set
    {
         RaisePropertyChanged("TestStr");
        _foobar = value;
    }
}
RaisePropertyChanged通知任何Listings视图(即绑定到它的文本框)某个属性已更改,并且应更新其内容。这是一个关键机制。”

因此,我将使用我最初尝试的内容,但添加RaisePropertyChanged以查看它的作用

更新

虽然我在MainViewModel.cs文件中实现了RaisedPropertyChanged(使用代码段mvvminpc),但这对序列化ViewModel中创建的任何内容仍然没有任何影响(与其他方面一样好)。我可能仍然做了一些错误的事情,但也可能是因为视图模型继承自受保护的类()。I(非常不情愿地)试着从