如何解决Silverlight:IsolatedStorageSettings用于在页面刷新之间持久保存数据
|| 我正在使用IsolatedStorageSettings
类来存储一些应在刷新Silverlight导航应用程序后保留的应用程序数据。
数据存储在FirstPage.xaml页中,并在SecondPage.xaml中检索。
如果不刷新,以下代码也可以正常工作。但是,如果我在SecondPage.xaml(第二页)上进行刷新,则值将从AppStore返回空。可能是什么原因。
public static class AppStore
{
private static IsolatedStorageSettings appSettings = IsolatedStorageSettings.ApplicationSettings;
public static String MyData
{
get
{
if (appSettings.Contains(\"MyData\"))
{
return(appSettings[\"MyData\"].ToString());
}
return String.Empty;
}
set
{
if (!appSettings.Contains(\"MyData\"))
{
appSettings.Add(\"MyData\",string.Empty);
}
appSettings[\"MyData\"] = value;
}
}
}
public partial class FirstPage : Page
{
private string data = \"somevalue\";
.
.
public FirstPage()
{
AppStore.MyData = data;
}
}
public partial class SecondPage : Page
{
public SecondPage()
{
ContentText.Text = AppStore.MyData;
}
}
解决方法
您没有将修改保存在IsolatedStorageSettings文件中,
你应该用这个
IsolatedStorageSettings.ApplicationSettings.Save();
请注意,您可以使用IsolatedStorageSettings.ApplicationSettings代替IsolatedStorageSettings的新实例。
也不会保存对设置的每次修改,只需在App.Exit()事件处理程序中调用此方法,将数据保存到硬盘上非常耗时。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。