C# 如何在Windows Phone 7中的独立存储中存储数据
我正在为windows phone 7构建应用程序。C# 如何在Windows Phone 7中的独立存储中存储数据,c#,windows-phone-7,C#,Windows Phone 7,我正在为windows phone 7构建应用程序。 我使用的是来自web服务的数据,我希望将其保存在独立的存储中,以便下次查看数据时,即使在脱机状态下也能显示数据 我的cs文件是: public about() { InitializeComponent(); KejriwalService.aapSoapClient myclient = new KejriwalService.aapSoapClient(); myclient.getarvindAboutComp
我使用的是来自web服务的数据,我希望将其保存在独立的存储中,以便下次查看数据时,即使在脱机状态下也能显示数据 我的cs文件是:
public about()
{
InitializeComponent();
KejriwalService.aapSoapClient myclient = new KejriwalService.aapSoapClient();
myclient.getarvindAboutCompleted += new EventHandler<KejriwalService.getarvindAboutCompletedEventArgs>(myclient_getarvindAboutCompleted);
myclient.getarvindAboutAsync();
}
void myclient_getarvindAboutCompleted(object sender, KejriwalService.getarvindAboutCompletedEventArgs e)
{
var data = e.Result;
XElement xml = XElement.Parse(data);
aboutview.Text = xml.Elements("UserDetails").Elements("about_details").First().Value;
}
public about()
{
初始化组件();
KejriwalService.aapSoapClient myclient=新的KejriwalService.aapSoapClient();
myclient.getarvindAboutCompleted+=新事件处理程序(myclient_getarvindAboutCompleted);
myclient.getarvindAboutAsync();
}
void myclient_getarvindAboutCompleted(对象发送方,KejriwalService.getarvindAboutCompletedEventArgs e)
{
var数据=e.结果;
XElement xml=XElement.Parse(数据);
aboutview.Text=xml.Elements(“UserDetails”).Elements(“about_details”).First().Value;
}
存储数据的最简单方法之一是将数据存储在隔离的存储设置中。您可以为此目的创建属性:
string PropertyName
{
get
{
var settings = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;
if (settings.Contains("valueKeyName"))
return (string)settings["valueKeyName"];
else
return null;
}
set
{
var settings = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;
if (settings.Contains("valueKeyName"))
settings["valueKeyName"] = value;
else
settings.Add("valueKeyName", value);
}
}
IsolatedStorageSettings将成对的键值存储在字典中。您只需选择存储值的键值名称和属性名称。借助SolatedStorageSettings,您可以以键值对的形式保存数据,如下所示:
var Iso_settings = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;
if (!Iso_settings.Contains("yourDataKey"))
{
Iso_settings.Add("yourDataKey", yourDataValue);
Iso_settings.Save()//This will save your data in isolated storage.
}
try
{
if (IsolatedStorageSettings.ApplicationSettings.Contains("email_id"))
{
IsolatedStorageSettings.ApplicationSettings["email_id"] = emailid;
IsolatedStorageSettings.ApplicationSettings["password"] = password;
IsolatedStorageSettings.ApplicationSettings.Save();
}
else
{
IsolatedStorageSettings.ApplicationSettings.Add("email_id", emailid);
IsolatedStorageSettings.ApplicationSettings.Add("password", password);
IsolatedStorageSettings.ApplicationSettings.Save();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
}
不要使用Sqlite如果您不想要脱机数据和存储数据两个最好的方法是使用隔离存储和linq,因此如果您只有1-2个数据成员,并且必须更改这些值,那么您可以使用隔离存储,否则,如果您有2-3列和行以表的形式存储数据,请使用linq。
隔离存储的使用方式如下:
var Iso_settings = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings;
if (!Iso_settings.Contains("yourDataKey"))
{
Iso_settings.Add("yourDataKey", yourDataValue);
Iso_settings.Save()//This will save your data in isolated storage.
}
try
{
if (IsolatedStorageSettings.ApplicationSettings.Contains("email_id"))
{
IsolatedStorageSettings.ApplicationSettings["email_id"] = emailid;
IsolatedStorageSettings.ApplicationSettings["password"] = password;
IsolatedStorageSettings.ApplicationSettings.Save();
}
else
{
IsolatedStorageSettings.ApplicationSettings.Add("email_id", emailid);
IsolatedStorageSettings.ApplicationSettings.Add("password", password);
IsolatedStorageSettings.ApplicationSettings.Save();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
}
对于linq,请点击链接。尝试使用支持windows phone的sqlite。您可以在每次需要解析数据时将xml数据保存到isolatedstorage中。这是一项复杂的行动。所以尝试使用sqlite。一旦解析数据并将数据保存在表格结构中。Iso_settings.save();//如果您想立即保存数据,然后立即读取,则不必这样做。但是,如果您只在应用程序的另一次启动时阅读它们,则不会,因为当您关闭应用程序时,IsolatedStorageSettings会保存。我可以共享另一个链接:。在本例中,对Save方法的调用没有详细说明。同样在这些页面上:在Silverlight for Windows Phone上,IsolatedStorageSettings不是线程安全的,当调用Save()时抛出IsolatedStorageException,你正在使用你的应用程序,在退出之前,你的应用程序遇到了一个异常,此时你的设置将无法保存。因此,请尽量避免出现异常。)如果你的应用程序遇到了异常,那么无论如何都是不好的。但我同意,在这种情况下,最好保存信息。我应该为valuekeyname替换什么。随你所需。它只是一个字符串键,指向您的值。例如,它可以是“ParsedXmlTextForAboutViewKey”(不需要在其中包含“…key”)。正如Pradeep Kesharwani在评论中所说,在整个应用程序中它必须是相同的