Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I mix Light and Dark themes in a C#/XAML Windows Store (Metro UI) App?

According to MSDN, the standard way of setting your app theme is to set RequestedTheme="Dark" or RequestedTheme="Light" on the toplevel Application instance.

This works great for simple apps but many times I find myself wanting to change the theme of an individual page or even an individual control (e.g. a light-themed text edit page vs a dark-themed image viewer in the same app).

XAML controls have 10s or 100s of visual states and color definitions and setting each one of them by hand is tedious and difficult to get 100% right!

Is there an easy way to set a dark or light theme on an individual control?

like image 841
ambrus Avatar asked Dec 21 '22 09:12

ambrus


1 Answers

For a XAML windows store app, the default look-and-feel of controls is defined in Common/StandardStyles.xaml. If you've ever looked at that file, you'll notice a ton of references like {StaticResource ApplicationForegroundThemeBrush}. Looks promising...

Unfortunately, these "theme brushes" aren't defined anywhere in your app, and there's no easy way to set a light or dark override for individual controls. However, there is an answer.

Fortunately, there's an excellent blog post by Joe White on the default theme colors, which I've turned into a resource dictionary that you can find here. Dropbox only does xml previews so you'll have to rename the file.

Copying these resources to your app won't help by itself though. To make use of them, you need to surgically override the default control styles to use them!

One way to do this is to create a new resource dictionary, e.g. at Common/CustomStyles.xaml, and merge that into the app's resources like so:

<Application
    x:Class="My.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    RequestedTheme="Light">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>

                <!-- 
                    Styles that define common aspects of the platform look and feel
                    Required by Visual Studio project and item templates
                 -->
                <ResourceDictionary Source="Common/ThemeColors.xaml"/>
                <ResourceDictionary Source="Common/StandardStyles.xaml"/>
                <ResourceDictionary Source="Common/CustomStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Notice that our default theme is Light. If we'd like to make a Dark-themed TextBlock, let's copy the visual style from StandardStyles.xaml and add it to our CustomStyles.xaml and make a few changes.

<Style x:Key="BasicRichTextStyleDark" TargetType="RichTextBlock">
    <Setter Property="Foreground" Value="{StaticResource ApplicationForegroundThemeBrushDark}"/>
    <Setter Property="FontSize" Value="{StaticResource ControlContentThemeFontSize}"/>
    <Setter Property="FontFamily" Value="{StaticResource ContentControlThemeFontFamily}"/>
    <Setter Property="TextTrimming" Value="WordEllipsis"/>
    <Setter Property="TextWrapping" Value="Wrap"/>
    <Setter Property="Typography.StylisticSet20" Value="True"/>
    <Setter Property="Typography.DiscretionaryLigatures" Value="True"/>
    <Setter Property="Typography.CaseSensitiveForms" Value="True"/>
</Style>

Notice that we have appended Dark to the style name and the theme brush definitions! You can do this via find and replace "ThemeBrush}" => "ThemeBrushDark}" in your CustomStyles.xaml file.

Now you can create a dark-themed text block like so:

<TextBlock Style="{StaticResource BasicRichTextStyleDark}"/>

Of course, you can use this technique for any other type of control as well. A little tedious, but the results are correct and it's not nearly as bad as trying to define every color manually!

There's no magic here. We're just defining some colors and overriding a default style with one that we copy-pasted and modified to use our colors.

like image 103
ambrus Avatar answered Apr 29 '23 22:04

ambrus