![]() ![]() / Switch the app's theme between light mode and dark mode, and save that setting. Private void SetThemeToggle(ElementTheme theme)Īnd this handles the toggle's being switched: / Set the theme toggle to the correct position (off for the default theme, and on for the non-default). The following method gets called when the page loads: This sets the theme according to the previous selection in app memory, and sets the toggle to match. Then, in the page constructor, added the following code:įrameworkElement root = (FrameworkElement) LOCALSETTINGS.Values = (int)value Įlse if ((int)value = (int)LOCALSETTINGS.Values) Throw new System.Exception("Only set the theme to light or dark mode!") Įlse if (LOCALSETTINGS.Values = null) LOCALSETTINGS.Values = (int)DEFAULTTHEME Įlse if ((int)LOCALSETTINGS.Values = (int)DEFAULTTHEME) / Gets or sets the current app colour setting from memory (light or dark mode). Static ApplicationDataContainer LOCALSETTINGS = Public const ElementTheme NONDEFLTHEME = ElementTheme.Dark Ĭonst string KEY_THEME = "appColourMode" Public const ElementTheme DEFAULTTHEME = ElementTheme.Light After more than a good afternoon's work, I was able to set the app's theme on the fly and keep it in memory for next startup, giving the user control through a ToggleButton.įirst, I made a settings class with a Theme property that automatically stores the current setting: I found axnull's answer most helpful, because it allows setting the theme while the app is running. I'm not able to provide code example right now (since i'm very busy today), but this is by far the most efficient way IMHO. In your BasePage's constructor listen to this event and apply changes when there's.Keep your settings under a static class and create an Event for keep tracking the model changes.Create your own BasePage (inherited from Page).My solution is doing theme updating on the Element level, not the Application level. There should be workarounds but they'll be very dumb.Microsoft doing this in their own official apps like Groove though. If you give the user an option to pick a theme that's part of app UI, you must save the setting in the app data and apply it when the app is restarted. Attempting to set RequestedTheme while the app is running throws an exception (NotSupportedException for Microsoft. The theme can only be set when the app is started, not while it’s running. This is really annoying I know and there's not much information about this situation other than this MSDN page : ![]() Application's RequestedTheme can only be updated in constructor.However (as you discovered), Page's RequestedTheme can be updated anytime in runtime.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |