[英]Silverlight: IsolatedStorageSettings to persist data between page refresh
我正在使用IsolatedStorageSettings
class 來存儲一些應用程序數據,這些數據應該在我的 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() 事件處理程序中調用此方法,將數據保存到硬盤非常耗時。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.