如何在终止应用程序后保留隔离存储设置



如何在应用程序启动时保留保存的隔离存储设置

我在后台事件中使用了终止异常:

     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 中声明的视图模型,但在启动时无法在其中获取隔离存储设置值。但它可以成功编译并运行。

你需要调用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");
    }
}

最新更新