C# 即使应用程序关闭,如何在windows phone中的隔离存储设置中保存数据?

C# 即使应用程序关闭,如何在windows phone中的隔离存储设置中保存数据?,c#,windows-phone-7,isolatedstorage,C#,Windows Phone 7,Isolatedstorage,我想存储一些基本信息,以防止每次登录,因为我使用的是IsolatedStorageSettings,但数据只存储到应用程序打开时,如何在应用程序关闭时保存数据,并在应用程序再次启动时返回。我的代码如下,用于存储我使用的信息 public static IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings; settings.Add("user", MainPage.user); 我用 if

我想存储一些基本信息,以防止每次登录,因为我使用的是IsolatedStorageSettings,但数据只存储到应用程序打开时,如何在应用程序关闭时保存数据,并在应用程序再次启动时返回。我的代码如下,用于存储我使用的信息

public static IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
settings.Add("user", MainPage.user);
我用

if (MainPage.settings.Contains("user"))
{
      MainPage.user = (User)MainPage.settings["user"];                            
}
此处MainPage.user是类MainPage中类user的静态对象。

您需要调用该方法:


我有两个假设:

1) 您不调用settings.Save()方法

2) 您正在使用emulator并将其关闭,这将导致所有设置失效

您的用户类应如下所示:

[DataContractAttribute]
public class User
    {
        [DataMemberAttribute]
        private String field;

        public String _Field
        {
            get { return field; }
            set { field = value; }
        }
    }

只需为每个要保存的字段设置[DataMemberAttribute]。

您可以使用此代码添加设置:

private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
appSettings.Add("user", "SOME_STRING_DATA");
private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
appSettings["user"] = "some_string_content";
private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
string value = (string)appSettings["user"];
要更新设置,请执行以下操作:

private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
appSettings.Add("user", "SOME_STRING_DATA");
private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
appSettings["user"] = "some_string_content";
private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
string value = (string)appSettings["user"];
要检索设置,请执行以下操作:

private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
appSettings.Add("user", "SOME_STRING_DATA");
private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
appSettings["user"] = "some_string_content";
private IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
string value = (string)appSettings["user"];
这在不使用Save()函数的情况下工作


我希望这就是您想要的。

IsolatedStorageSettings将数据存储在“键值”对中,要保存数据,请使用IsolatedStorageSettings.save()方法:

要检索设置值,请将其转换为所需的数据类型:

IsolatedStorageSettings lastOpenedFileSettings =IsolatedStorageSettings.ApplicationSettings;
 string lastOpenedFileId = (string)lastOpenedFileSettings["yourSeetingKey"];

看起来代码中的每件事都很好,除了下面的代码行。定义IsolatedStorageSettings对象时删除静态。你的代码应该是

public IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
并在app.xaml.cs文件中进行以下更改

 private void Application_Deactivated(object sender, DeactivatedEventArgs e)
        {
           // SettingView.LiveClient = null;
            IsolatedStorageSettings.ApplicationSettings.Save();
        }

        // Code to execute when the application is closing (eg, user hit Back)
        // This code will not execute when the application is deactivated
        private void Application_Closing(object sender, ClosingEventArgs e)
        {
            IsolatedStorageSettings.ApplicationSettings.Save();
        } 

您使用真实设备还是仿真器?您使用JSON数据吗?如果是,您在保存之前是否正确地反序列化了它?是的,我使用了json,我可以序列化和反序列化它!所以现在我不需要使用DataContractAttribute和settings.save()了!谢谢你的支持!我在使用设置时遇到此错误。无法序列化save()类型“vUrja_Mobile_Json.Device”。考虑用DATACONTractAtvices属性标记它,并标记您想用DATAMEMBAREATION属性序列化的所有成员。@ KEVAN:这里有一篇文章详细说明了您想做什么:这也是有用的!谢谢!如果无法保存类,则应使用[DataContractAttribute]或[DataContract]标记该类以及要使用[DataMemberAttribute]或[DataMember]保存的字段。每次在使用设置时出现此错误时,我都将关闭emulator。save()类型“vUrja_Mobile_Json.Device”无法序列化。考虑使用DATACONTractAttor属性对其进行标记,并标记要用DATAMEMBRITE属性属性序列化的所有成员。您可以告诉我这个属性是在哪个程序集中给出的,因为我正在得到这个错误。找不到类型或命名空间名称“DataContractAttribute”(您是否缺少using指令或程序集引用?)System.Runtime.Serializationbtw,在Google中编写DataContractAttribute将使您更快地找到正确的程序集。这看起来与OP编写的代码几乎相同。看不出有什么区别。他提到使用
settings.Save()我没有在哪里提到过。