如何在终止应用程序后持久化IsolatedStorageSettings

时间:2012-11-14 10:05:06

标签: windows-phone-7 isolatedstorage exit-code

如何在应用程序启动时保留已保存的isolatedstoragesetting

我使用异常来终止事件:

     protected void _BackKeyPress(object sender, CancelEventArgs e)
    {
        if (MessageBox.Show("Do you want to close the application?", "Q", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
        {
            e.Cancel = true;
            System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.Add("key2", "33r4 ");
        }
        else
        {
           if (IsolatedStorageSettings.ApplicationSettings.Contains("Key"))
        {
            IsolatedStorageSettings.ApplicationSettings["Key"] = App.Current.ViewModel;
        }
        else
        {
            IsolatedStorageSettings.ApplicationSettings.Add("Key", App.Current.ViewModel);
        }
           throw new Exception("ExitApplication");
        }
    }

我尝试保存在app.xaml.cs中声明的viewmodel,但是在启动时无法获取其中的isolatedstorage设置值。但它成功编译并运行。

1 个答案:

答案 0 :(得分:1)

您需要调用IsolatedStorageSettings.Save方法:

protected void _BackKeyPress(object sender, CancelEventArgs e)
{
    if (MessageBox.Show("Do you want to close the application?", "Q", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
    {
        e.Cancel = true;
        System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.Add("key2", "33r4 ");
        IsolatedStorageSettings.Save();
    }
    else
    {
        if (IsolatedStorageSettings.ApplicationSettings.Contains("Key"))
        {
            IsolatedStorageSettings.ApplicationSettings["Key"] = App.Current.ViewModel;
        }
        else
        {
            IsolatedStorageSettings.ApplicationSettings.Add("Key", App.Current.ViewModel);
        }
        IsolatedStorageSettings.Save();
        throw new Exception("ExitApplication");
    }
}