我正在使用切换开关,直到我重新启动应用程序才能正常工作。重新启动应用程序后,切换开关不会保留以前的更改并恢复为默认状态。我该如何解决这个问题?
更新:这是我尝试将切换开关值保存到本地设置的方式。
private void ThemeToggle_switch(object sender, RoutedEventArgs e)
{
if (ThemeToggle.IsOn)
{
(Frame).RequestedTheme = ElementTheme.Light;
Windows.Storage.ApplicationData.Current.LocalSettings.Values["ThemeToggle"] = "Light";
Windows.Storage.ApplicationData.Current.LocalSettings.Values["ThemeToggleValue"] = "on";
}
else (Frame).RequestedTheme = ElementTheme.Dark;
Windows.Storage.ApplicationData.Current.LocalSettings.Values["ThemeToggle"] = "Dark";
Windows.Storage.ApplicationData.Current.LocalSettings.Values["ThemeToggleValue"] = "off";
}
在App.xamls.cs中,我按如下方式设置了本地设置值:
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
localSettings.Values["ThemeToggle"] = "Dark";
localSettings.Values["ThemeToggleValue"] = "off";
逻辑工作正常,切换开关正常,主题正在改变。但是,只要我关闭并重新启动应用程序,我的应用程序就会保留所选主题,但切换按钮不会保持其状态。这就是我的Xaml代码的样子:
<ToggleSwitch x:Uid="ThemeToggle" x:Name="ThemeToggle" OnContent="Dark Theme" OffContent="Light Theme" Toggled="ThemeToggle_switch" HorizontalContentAlignment="Stretch" Margin="10,20,6,10" Height="65"/>
我怀疑这可能与在本地设置中添加/替换/读取值有关,但我对如何修复它毫无头绪。任何帮助表示赞赏。