Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Terminate an Application Properly and Save Data
    primarykey
    data
    text
    <p>I have recently come across the <code>App.Current.Terminate</code> method, which I would like to use in an application. Before calling this, however, I need to save some data. For some reason, even though the save seems to be shown as effective, once the application is terminated the old settings are restored. I cannot figure out what is going on. I have a ListPicker, and I am trying to change the theme setting in my application between light and dark, using Jeff Wilcox's <code>ThemeManager</code>. </p> <p>SettingsPage.xaml</p> <pre><code>&lt;Grid.Resources&gt; &lt;DataTemplate x:Name="PickerItemTemplate"&gt; &lt;StackPanel Tap="stk_Tap"&gt; &lt;TextBlock Text="{Binding Name}"/&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/Grid.Resources&gt; ... &lt;toolkit:ListPicker x:Name="ThemeListPicker" Header="Theme" ItemTemplate="{StaticResource PickerItemTemplate}"/&gt; </code></pre> <p>SettingsPage.xaml.cs</p> <pre><code>List&lt;Theme&gt; themeList; private int currentThemeIndex; protected override void OnNavigatedTo(NavigationEventArgs e) { base.OnNavigatedTo(e); themeList = new List&lt;Theme&gt;(); 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(); } </code></pre> <p>Theme.cs</p> <pre><code>public class Theme { public string Name { get; set; } public string name //name to use in determining current theme { get; set; } } </code></pre> <p>Settings.cs //writes key/value pair to IsoStore</p> <pre><code>//Theme - set to true by default public static readonly Setting&lt;bool&gt; LightTheme = new Setting&lt;bool&gt;("LightTheme", true); </code></pre> <p>App.xaml.cs</p> <pre><code>public App() { ... //Theme Manager if (Settings.LightTheme.Value) ThemeManager.ToLightTheme(); else ThemeManager.ToDarkTheme(); ... } </code></pre> <p>The main problem is that the theme never changes. I set a breakpoint in App.xaml.cs to check the current value of <code>Settings.LightTheme.Value</code> on application load, which always stays as true which sets the app to the light theme. In SettingsPage.xaml.cs when a user selects either the light or dark option in the ListPicker, I detect that the value of <code>Settings.LightTheme.Value</code> switches between true and false. Immediately after, since the user must select OK on the MessageBox to restart the application, the app is terminated. Upon restarting, <code>Settings.LightTheme.Value</code> is true. I am not sure how to fix this?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload