1

I am trying to make a UWP app (in Visual Studio 2019) which has a NavigationView (Top). Each NavigationViewItem has a different page associated with it/written for it.

I am trying to put an app-wide dark mode toggle-switch (which switches the app's theme dynamically/on the spot/at runtime). The following is my code to do that, which also generates an error in an auto-generated file called App.g.i.cs:

// App.xaml.cs

/*
.
.
.
*/
using Windows.UI.Xaml;

// namespace AppName
// {
sealed partial class App : Application
{
    /*
     * Default/Auto-generated initial code
     */

    // `static` for inter-app usage/to use these functions in a different app C# file/class
    public static ApplicationTheme GetApplicationTheme()
    {
        return App.Current.RequestedTheme;
    }

    public static void SetApplicationTheme(ApplicationTheme applicationTheme)
    {
        App.Current.RequestedTheme = applicationTheme;
    }

    // Made this to try to remove the error, but failed
    public static bool IsDarkModeEnabled()
    {
        return GetApplicationTheme() == ApplicationTheme.Dark;
    }

    // This too was made as an attempt to remove the error, but failed as well
    public static void SwitchApplicationTheme()
    {
        if (IsDarkModeEnabled())
        {
            SetApplicationTheme(ApplicationTheme.Light);
        }
        else
        {
            SetApplicationTheme(ApplicationTheme.Dark);
        }
    }
}
// }
<!-- Settings.xaml -->

<!-- <Page Loaded="Page_Loaded"
           ...> -->
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*" />
            <RowDefinition Height="9*" />
        </Grid.RowDefinitions>

        <ToggleSwitch x:Name="DarkModeToggleSwitch"
                      Header="Dark mode"
                      Grid.Row="0"
                      Toggled="DarkModeToggleSwitch_Toggled" />
    </Grid>
<!-- </Page> -->
// Settings.xaml.cs

/*
.
.
.
*/
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

// namespace AppName
// {
public sealed partial class Settings : Page
{
    /*
     * Default/Auto-generated initial code
     */

    private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        DarkModeToggleSwitch.IsOn = App.IsDarkModeEnabled();
    }

    private void DarkModeToggleSwitch_Toggled(object sender, RoutedEventArgs e)
    {
        App.SwitchApplicationTheme();
    }
}
// }

The error is generated when -

  • navigating to the Settings page (in system dark mode), and
  • when clicking DarkModeToggleSwitch (in system light mode)

This makes me assume its because of toggle-switching programmatically to dark mode, and I don't understand why that is happening

This is the error raised in App.g.i.cs due to the above reasons: The error in App.g.i.cs This is the elongated error: The error viewed as text

Also, please let me know if there is a way to add RevealBrush to the default Settings icon button in the top NavigationView (because it is not used/enabled by default)

Please help me with this! Thank you!

PS: Any doubts regarding the question will be answered in comments or through question edits.

SFM61319
  • 134
  • 3
  • 13
  • Does this answer your question? [Changing Theme in Windows 10 UWP App Programmatically](https://stackoverflow.com/questions/34554871/changing-theme-in-windows-10-uwp-app-programmatically) – Tom Joney Jul 17 '20 at 20:56
  • No, @TomJoney, that did not answer my question correctly. The functions I've used are static methods, which means I can't use `this` in the method definition. And if i remove the `static` keyword to make use of `this`, then i can't use the method in any other class/page/c# file. Also, I could'nt use `AppSettings` as it was nowhere in the context, not even in any other namespaces (else I would've used `using` to use the containing namespace) – SFM61319 Jul 17 '20 at 21:02
  • Oh wait, nvm. I will try to re-write my code as [given here](https://stackoverflow.com/a/56918252/11588707) thanks to you, @TomJoney! – SFM61319 Jul 17 '20 at 21:17
  • Hi, can the methond from the link you provided solve your issue? – Faywang - MSFT Jul 20 '20 at 05:07
  • @Faywang-MSFT, unfortunately it did not. MS should've made it easier to toggle dark/light mode when implementing dark and light modes. I'm really sorry for being **THIS** late – SFM61319 Jul 28 '20 at 14:49
  • You can check the Remark part of this [document](https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.application.requestedtheme?view=winrt-19041#remarks), it mentiones "The theme can only be set when the app is started, not while it's running. Attempting to set RequestedTheme while the app is running throws an exception. 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". – Faywang - MSFT Jul 29 '20 at 08:20
  • @Faywang-MSFT, then how do many apps change the app theme without restarting the app? Do they just apply the theme to the elements and not to the entire app? – SFM61319 Aug 01 '20 at 09:59
  • Yes, maybe you could try to set this.RequestedTheme on the page you want to apply the theme. – Faywang - MSFT Aug 04 '20 at 07:32

0 Answers0