我最近遇到了App.Current.Terminate
方法,我想在应用程序中使用它。但是,在调用它之前,我需要保存一些数据。出于某种原因,即使保存似乎显示为有效,一旦应用程序终止,旧设置将被恢复。我无法弄清楚发生了什么。我有一个ListPicker,我正在尝试使用Jeff Wilcox的ThemeManager
在光明和黑暗之间更改我的应用程序中的主题设置。
SettingsPage.xaml
<Grid.Resources>
<DataTemplate x:Name="PickerItemTemplate">
<StackPanel Tap="stk_Tap">
<TextBlock Text="{Binding Name}"/>
</StackPanel>
</DataTemplate>
</Grid.Resources>
...
<toolkit:ListPicker x:Name="ThemeListPicker" Header="Theme"
ItemTemplate="{StaticResource PickerItemTemplate}"/>
SettingsPage.xaml.cs
List<Theme> themeList;
private int currentThemeIndex;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
themeList = new List<Theme>();
themeList.Add(new Theme() { Name = "light", name = "light" }); //Name subject to change
themeList.Add(new Theme() { Name = "dark", name = "dark" }); //Name subject to change
ThemeListPicker.ItemsSource = themeList;
//make sure ThemeListPicker shows current theme when NavigatedTo
if (Settings.LightTheme.Value)
{
ThemeListPicker.SelectedIndex = 0;
currentThemeIndex = 0;
}
else
{
ThemeListPicker.SelectedIndex = 1;
currentThemeIndex = 1;
}
}
private void stk_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
if (ThemeListPicker.SelectedIndex != -1)
{
//avoid asking the question if the selected item is the same as the current item
//doesn't quite work properly? Skips when the item is changed for the first time
if(ThemeListPicker.SelectedIndex != currentThemeIndex)
{
MessageBoxResult result = MessageBox.Show("The app must be restarted to apply theme changes."", "", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
var selectedItem = (Theme)(sender as StackPanel).DataContext;
switch (selectedItem.name)
{
case "light":
Settings.LightTheme.Value = true;
currentThemeIndex = 0;
break;
case "dark":
Settings.LightTheme.Value = false;
currentThemeIndex = 1;
break;
default:
Settings.LightTheme.Value = true;
currentThemeIndex = 0;
break;
}
Terminate();
}
}
}
private void Terminate()
{
App.Current.Terminate();
}
Theme.cs
public class Theme
{
public string Name
{
get;
set;
}
public string name //name to use in determining current theme
{
get;
set;
}
}
Settings.cs //将键/值对写入IsoStore
//Theme - set to true by default
public static readonly Setting<bool> LightTheme = new Setting<bool>("LightTheme", true);
App.xaml.cs
public App()
{
...
//Theme Manager
if (Settings.LightTheme.Value)
ThemeManager.ToLightTheme();
else
ThemeManager.ToDarkTheme();
...
}
主要问题是主题永远不会改变。我在App.xaml.cs中设置了一个断点,以检查应用程序加载时Settings.LightTheme.Value
的当前值,该值始终保持为true,将应用程序设置为灯光主题。在SettingsPage.xaml.cs中,当用户选择ListPicker中的浅色或深色选项时,我检测到Settings.LightTheme.Value
的值在true和false之间切换。紧接着,由于用户必须在MessageBox上选择OK以重新启动应用程序,应用程序将被终止。重新启动后,Settings.LightTheme.Value
为真。我不知道如何解决这个问题?
答案 0 :(得分:1)
终止前请致电 IsolatedStorageSettings.Save()。
答案 1 :(得分:0)
在App.xaml.cs中,可以找到以下方法。只需将“主题保存”代码添加到此方法中即可。
// Code to execute when the application is closing (eg, user hit Back)
// This code will not execute when the application is deactivated
private void Application_Closing(object sender, ClosingEventArgs e)
{
//theme saving code goes here
}