Edit

Share via


Theme Listener

KNOWN ISSUE: ThemeListener might not work in WinUI3 applications.

<!--  Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE file in the project root for more information.  -->
<Page x:Class="HelpersExperiment.Samples.ThemeListenerSample"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:local="using:HelpersExperiment.Samples"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d">

    <StackPanel>
        <TextBlock>
            <Run FontWeight="SemiBold"
                 Text="System theme: " />
            <Run x:Name="SystemTheme" />
        </TextBlock>

        <TextBlock>
            <Run FontWeight="SemiBold"
                 Text="Sample theme: " />
            <Run x:Name="CurrentTheme" />
        </TextBlock>
    </StackPanel>
</Page>
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using CommunityToolkit.WinUI.Helpers;

namespace HelpersExperiment.Samples;

[ToolkitSample(id: nameof(ThemeListenerSample), "Theme Listener", description: $"A sample for showing how to use {nameof(ThemeListener)}.")]
public sealed partial class ThemeListenerSample : Page
{
    public ThemeListenerSample()
    {
        this.InitializeComponent();
        Listener = new ThemeListener();
        this.Loaded += ThemeListenerPage_Loaded;
        Listener.ThemeChanged += Listener_ThemeChanged;
        this.ActualThemeChanged += this.ThemeListenerSample_ActualThemeChanged;
    }

    private void ThemeListenerSample_ActualThemeChanged(FrameworkElement sender, object args)
    {
        UpdateThemeState();
    }

    private void ThemeListenerPage_Loaded(object sender, RoutedEventArgs e)
    {
        UpdateThemeState();
    }

    private void Listener_ThemeChanged(ThemeListener sender)
    {
        UpdateThemeState();
    }

    private void UpdateThemeState()
    {
        SystemTheme.Text = Listener.CurrentThemeName;
        CurrentTheme.Text = this.ActualTheme.ToString();
    }

    public ThemeListener Listener { get; }
}