microsoft-ui-xaml: UISettings.ColorValuesChanged not fired

Describe the bug

The UISettings.ColorValuesChanged event does not work for me.

Steps to reproduce the bug

  1. Create empty project
  2. In MainWindow, add the following code:
public MainWindow()
{
    InitializeComponent();
    var settings = new UISettings();
    settings.ColorValuesChanged += Settings_ColorValuesChanged;
}

private void Settings_ColorValuesChanged(UISettings sender, object args)
{
    Debug.WriteLine("Theme changed");
}
  1. Change Windows colors, nothing will be printed in the output

Expected behavior

Theme changed printed in the output.

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 1.4.5: 1.4.240411001

Windows version

Windows 10 (21H2): Build 19044

Additional context

Actual Windows build: 19045.3930

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (1 by maintainers)

Most upvoted comments

Ok that’s clear. So, my question will become a feature request: may we have an official and supported C#/WindowsAppSDK API that allows to listen for theme changes? Of course, I expect this API to work on any both packaged and unpackaged applications and on supported OSes (win11 and win10, which maintains an abundant market share w.r.t. win11).

For just the Theme , there is Microsoft.UI.Xaml.FrameworkElement.ActualThemeChanged It works for packaged/unpackaged (at least on Windows 10) :

            FrameworkElement root = (FrameworkElement)this.Content;
            root.ActualThemeChanged += Root_ActualThemeChanged;
        private void Root_ActualThemeChanged(FrameworkElement sender, object args)
        {
            Console.Beep(5000, 10);
            Debug.WriteLine("Theme changed");
        }

I think the issue should be reopened because the provided solution seems temporary: I think that the official documented C# APIs should provide an event that notifies when the user change system colors. I don’t care if the name is UISettings.ColorValuesChanged or whatever other class/event name, but this remains a basic feature for modern systems.

For anyone that will have the same question, here’s how I did it in C# using the WinUIEx library:

MainWindow.xaml.cs

private readonly WindowMessageMonitor monitor;
private readonly DispatcherQueue dispatcherQueue;

public MainWindow()
{
    monitor = new WindowMessageMonitor(this);
    monitor.WindowMessageReceived += Monitor_WindowMessageReceived;
    dispatcherQueue = DispatcherQueue.GetForCurrentThread();

    //...
}

private void Monitor_WindowMessageReceived(object? sender, WindowMessageEventArgs e)
{
    if (e.Message.MessageId == 0x001A) //WM_SETTINGCHANGE
    {
        dispatcherQueue.TryEnqueue(() => TitleBarHelper.UpdateTitleBar(ElementTheme.Default));
    }
}

TitleBarHelper is the default helper generated with Template Studio for WinUI.

I couldn’t replicate the C++ code fully, because there’s no string in the event args, so I don’t have anything to compare “ImmersiveColorSet” to, so I suspect the method is called more often than necessary.