我无法保存到独立存储? [英] I'm not able to save to the isolated storage?

查看:21
本文介绍了我无法保存到独立存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的模型保存在独立存储中:

I'm trying to save my model in isolated storage:

var settings = IsolatedStorageSettings.ApplicationSettings;

CurrentPlaceNowModel model = new CurrentPlaceNowModel();

settings.TryGetValue<CurrentPlaceNowModel>("model", out model);

if (model == null)
{
    MessageBox.Show("NULL");
    settings.Add("model", new CurrentPlaceNowModel());
    settings.Save();
}
else
    MessageBox.Show("NOT NULL");

当我启动 emu 时,我当然是NULL",但是如果我关闭 emu 上的应用程序并从菜单中重新启动它(而不是在 Visual Studio 中再次启动它),为什么我会一直得到它.

When I start the emu i ofcourse the "NULL", but why do I keep getting it if I close the app on the emu and start it again from the menu (NOT starting it again in Visual Studio).

我不应该第二次得到NOT NULL"吗?

Should I not get "NOT NULL" 2nd time around?

推荐答案

我会以不同的方式执行此操作并进行特定检查以查看密钥是否存在.

I'd do this differently and make a specific check to see if the key exists.

CurrentPlaceNowModel model; 

using (var settings = IsolatedStorageSettings.ApplicationSettings)
{
    if (settings.Contains("MODEL"))
    {
        model = settings["MODEL"] as CurrentPlaceNowModel;
    }
    else
    {
        model = new CurrentPlaceNowModel();
        settings.Add("MODEL", model);    
        settings.Save();
    }
}

这种使用隔离存储的模式绝对有效.

This pattern of working with IsolatedStorage definitely works.

这不起作用的唯一原因是 CurrentPlaceNowModel 无法使用 DataContractSerializer 进行序列化.这是 ApplicationSettings 在内部用于序列化对象的内容.
您可以通过自己以这种方式序列化来测试它,看看会发生什么.

The only reason that this wouldn't work would be if CurrentPlaceNowModel could not be serialized with the DataContractSerializer. This is what the ApplicationSettings uses internally to serialize objects.
You can test this by serialising it this way yourself to see what happens.

这篇关于我无法保存到独立存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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