为什么独立存储没有保留在我的 WP7 应用程序中?

发布于 2024-10-05 16:50:35 字数 1379 浏览 3 评论 0原文

我正在为我的应用程序使用IsolatedStorageSettings.ApplicationSettings。与独立存储相关的所有代码都发生在我的 Application_Launching、Application_Activated、Application_Closing 和 Application_Deactivated 方法中,如下所示:

public IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;


private void Application_Launching(object sender, LaunchingEventArgs e)
{
       if (settings.Contains("myObjList"))
       {
           App.ObjList = (ObservableCollection<myObj>)settings["myObjList"];
       }
       else
       {
            settings.Add("myObjList", App.ObjList);
       }
}

private void Application_Activated(object sender, ActivatedEventArgs e)
{
   if (settings.Contains("myObjList"))
   {
       App.ObjList = (ObservableCollection<myObj>)settings["myObjList"];
   }
   else
   {
       settings.Add("myObjList", App.ObjList);
   }
}
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{

    settings["myObjList"] = App.ObjList;
    settings.Save();
}
private void Application_Closing(object sender, ClosingEventArgs e)
{
    settings["myObjList"] = App.ObjList;
    settings.Save();
}

所有这些都发生在默认情况下随每个新应用程序创建的 App.xaml.cs 文件中。

我尝试使用后退按钮以及使用窗口按钮退出应用程序。让模拟器保持运行,我尝试使用后退按钮重新打开应用程序,然后导航到应用程序列表并打开。

我遇到的问题是,在加载或激活时,settings.Contains["myObjList"] 返回 false 并继续将密钥重新添加到设置中。

有谁明白为什么我的设置键(和值)不持久?

I am using IsolatedStorageSettings.ApplicationSettings for my application. All code associated with Isolated storage occurs in my Application_Launching, Application_Activated, Application_Closing, and Application_Deactivated methods as follows:

public IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;


private void Application_Launching(object sender, LaunchingEventArgs e)
{
       if (settings.Contains("myObjList"))
       {
           App.ObjList = (ObservableCollection<myObj>)settings["myObjList"];
       }
       else
       {
            settings.Add("myObjList", App.ObjList);
       }
}

private void Application_Activated(object sender, ActivatedEventArgs e)
{
   if (settings.Contains("myObjList"))
   {
       App.ObjList = (ObservableCollection<myObj>)settings["myObjList"];
   }
   else
   {
       settings.Add("myObjList", App.ObjList);
   }
}
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{

    settings["myObjList"] = App.ObjList;
    settings.Save();
}
private void Application_Closing(object sender, ClosingEventArgs e)
{
    settings["myObjList"] = App.ObjList;
    settings.Save();
}

All of this is occurring in the App.xaml.cs file that is created by default with every new application.

I have tried exiting the application using the back button as well as by using the windows button. Leaving the emulator running, I tried reopening the application using the back button, and navigating to the application list and opening.

The issue I am having is that on load or activation the settings.Contains["myObjList"] is returning false and proceeding to add the key to settings all over again.

Does anyone see why my settings key (and value) is not persisting?

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

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

发布评论

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

评论(2

深巷少女 2024-10-12 16:50:35

你的类 myObj 可序列化吗?根据经验,如果不是,则不会抛出任何错误,它只是不会添加到isolatedStorage中。

您可以使用类中的 DataContract 和 DataMember 属性来启用此功能,如下所示。

[DataContract]
public class myObj
{
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Id { get; set; }

    [DataMember]
    public string Phone { get; set; }

Is your class myObj Serializable? From experience if it's not then no error will be thrown it simply isn't added to IsolatedStorage.

You can use the DataContract and DataMember attributes in your class to enable this as follows.

[DataContract]
public class myObj
{
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Id { get; set; }

    [DataMember]
    public string Phone { get; set; }
皇甫轩 2024-10-12 16:50:35

我看到一些问题:

  1. IsolatedStorageSettings 文档明确表示不要调用 Save(),因为它不是线程安全的(向下滚动到 WP 的平台说明)并且可能引发异常(并导致您的设置不保存)。

  2. 这里的情况似乎并非如此,但到处使用字符串“myObjList”非常危险,因为它很容易拼写错误。我会将它放在一个常量中并排除任何键入错误

  3. In我的经验isolatedStorageSettings 在当前的 WP7 版本上不是很强大。您最好创建一个类并将其序列化到IsolatedStorage 文件中。无论如何,继续使用您的应用程序,您可能会保存更多内容,并且这样您将获得更清晰的代码。

I see some issues:

  1. The IsolatedStorageSettings doc explicitly says not to call Save() because it's not thread safe (scroll down to the platform notes for WP) and may raise an exception (and cause your settings not to be saved).

  2. It seems not to be the case here, but using the string "myObjList" all around is pretty dangerous as it's easy to mispell. I would put it inside a constant and rule out any typing error

  3. In my experience IsolatedStorageSettings is not very robust on the current WP7 version. You better create a class and serialize it into an IsolatedStorage file. Anyways going on with your app you will probably have more things to save and you will have cleaner code that way.

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