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:
This is the elongated error:
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.