Reputation: 59
I'm making a App in .NET Maui and I was wondering If I could somehow only have one theme instead of dark and light mode themes, as I want all of my users to have the same view. How do I maintain one theme?
I tried playing around with the colors but no success as they are mostly bound to Resources/Styles/Styles.xaml.
Edit:
In the /Resources/Styles/Styles.xaml I removed every {AppThemeBinding Light={StaticResource Gray950}, Dark={StaticResource Gray200}}
and replaced it with {StaticResource Gray950}
so that every element would have the light mode style. However some elements are still black and I can't change the styles via the Style Setter:
<Style TargetType="TextCell">
<Setter Property="TextColor" Value="Black" />
<Setter Property="DetailColor" Value="Black" />
</Style>
Upvotes: 1
Views: 1195
Reputation: 11
According to MS documentation,
The theme used by the app can be set with the Application.UserAppTheme property, which is of type AppTheme, regardless of which system theme is currently operational:
Application.Current.UserAppTheme = AppTheme.Dark;
Upvotes: 1
Reputation: 1347
Yes, it is possible to maintain a single theme across your .NET MAUI application instead of supporting both light and dark modes. All you have to do is define your color resources in App.xaml (or elsewhere) without differentiating based on the requested theme. This way, your app will ignore the system setting for light/dark mode and always use the colors you've defined.
Here's a sample of setting color resources in App.xaml:
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="YourAppNamespace.App">
<Application.Resources>
<ResourceDictionary>
<Color x:Key="PrimaryColor">#da8f8f</Color>
<Color x:Key="SecondaryColor">#fa968e</Color>
<!-- And so on, for each color you use -->
</ResourceDictionary>
</Application.Resources>
</Application>
Then, simply call these resources in your styles (or directly from the components) like this:
<Button BackgroundColor="{DynamicResource PrimaryColor}"/>
Or in a style:
<Style TargetType="Button">
<Setter Property="BackgroundColor" Value="{DynamicResource PrimaryColor}"/>
</Style>
This way, you maintain the same colors regardless of the current system theme.
Upvotes: 0