WindowsCommunityToolkit icon indicating copy to clipboard operation
WindowsCommunityToolkit copied to clipboard

ThemeListener.ThemeChanged does not work in WinUI 3

Open yoshiask opened this issue 3 years ago • 7 comments
trafficstars

Describe the bug

ThemeListener seems to do nothing in WinAppSDK apps. A comment in a related issue points out that the events ThemeListener relies on are no longer supported in WinUI 3.

  • [x] Is this bug a regression in the toolkit? If so, what toolkit version did you last see it work: 7.1.2 for UWP

Steps to Reproduce

  • [x] Can this be reproduced in the Sample App? (Either in a sample as-is or with new XAML pasted in the editor.) If so, please provide custom XAML or steps to reproduce. If not, let us know why it can't be reproduced (e.g. more complex setup, environment, dependencies, etc...)

Steps to reproduce the behavior:

Place the following code anywhere inside a WinAppSDK 1.0.0 C# app:

private void ThemeChanged(ThemeListener sender)
{
    ApplicationTheme theme = sender.CurrentTheme;
    Debug.WriteLine(theme);
}
ThemeListener themeListener = new();
themeListener.ThemeChanged += ThemeChanged;

Observe that the ThemeChanged handler is never fired.

Expected behavior

The ThemeChanged event should fire when the system theme is changed.

Environment

NuGet Package(s): CommunityToolkit.WinUI.UI

Package Version(s): 7.1.2

Windows 10 Build Number:

  • [ ] Fall Creators Update (16299)
  • [ ] April 2018 Update (17134)
  • [ ] October 2018 Update (17763)
  • [ ] May 2019 Update (18362)
  • [ ] May 2020 Update (19041)
  • [x] Insider Build (22504.1010)

App min and target version:

  • [ ] Fall Creators Update (16299)
  • [ ] April 2018 Update (17134)
  • [ ] October 2018 Update (17763)
  • [ ] May 2019 Update (18362)
  • [ ] May 2020 Update (19041)
  • [x] Insider Build (22000)

Device form factor:

  • [x] Desktop
  • [ ] Xbox
  • [ ] Surface Hub
  • [ ] IoT

Visual Studio version:

  • [ ] 2017 (15.{minor_version})
  • [ ] 2019 (16.{minor_version})
  • [x] 2022 (17.0.2)

yoshiask avatar Dec 03 '21 02:12 yoshiask

Hello yoshiask, thank you for opening an issue with us!

I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible. Other community members may also look into the issue and provide feedback 🙌

ghost avatar Dec 03 '21 02:12 ghost

I believe this is still a known issue as there's no corresponding API yet in the WinAppSDK for this, right @azchohfi?

Not sure how we should best mark things that we know aren't working yet...

michael-hawker avatar Dec 03 '21 19:12 michael-hawker

Yes, these events are not supported: https://docs.microsoft.com/en-us/windows/apps/desktop/modernize/desktop-to-uwp-supported-api#events

I don't know for sure, but I believe it will be possible to track this when they add the EnvironmentVariableChangeTracker. It should be out on 1.1, but I'm not 100% sure.

azchohfi avatar Dec 03 '21 20:12 azchohfi

@azchohfi @zateutsch this is another case similar to #4356 where there's a missing API or an API that doesn't work in unpackaged. I'm not sure if there's system APIs that are in similar places and get annotated as such somehow? Is there any better attributes or practice we should be doing here to better indicate what's not going to work currently with WinUI 3/WASDK?

michael-hawker avatar Dec 14 '21 21:12 michael-hawker

这是我的解决方案,可能有些笨拙。欢迎大佬献策。 Here is my solution, probably a little clumsy. Welcome the big guy to contribute.

我使用visual studio 2022创建了一个新的空白应用,使用的是Windows app sdk 1.1.5,经过测试发现,当系统主题发生变化时,UISettings().ColorValuesChanged事件仍然能够被触发。在事件触发后,可以读取注册表 “计算机\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize”项的两个键值,其中“AppsUseLightTheme”对应的是“默认应用模式”的设置,“SystemUsesLightTheme”对应的是“默认Windows模式“的设置。 I created a new blank app using Visual Studio 2022 using Windows app SDK 1.1.5 and tested UISettings() when the system theme changes. The ColorValuesChanged event can still be triggered. After the event triggered, you can read the two keys of the registry "Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize" item, where "AppsUseLightTheme" corresponds to the setting of "Default App Mode", "SystemUsesLightTheme" Corresponding to the setting of "Default Windows Mode".

AppsUseLightTheme 值为0时,是深色主题,1时是浅色主题。 AppsUseLightTheme is a dark theme when the value is 0 and light theme is at 1.

这是我测试的代码。 Here is the code I tested.

MainWindow.xaml

<Window x:Class="App1.MainWindow" 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:App1" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">

<StackPanel
    HorizontalAlignment="Center"
    VerticalAlignment="Center"
    Orientation="Horizontal">
    <Button x:Name="myButton" Content="{x:Bind TestString, Mode=OneWay}" />
</StackPanel>

MainWindow.xaml.cs

using System; using System.ComponentModel; using Microsoft.UI.Dispatching; using Microsoft.UI.Xaml; using Microsoft.Win32; using Windows.UI.ViewManagement;

// To learn more about WinUI, the WinUI project structure, // and more about our project templates, see: http://aka.ms/winui-project-info.

namespace App1;

///

/// An empty window that can be used on its own or navigated to within a Frame. /// public sealed partial class MainWindow : Window, INotifyPropertyChanged { private string testString = string.Empty;

private DispatcherQueue dispatcherQueue = DispatcherQueue.GetForCurrentThread();

public string TestString
{
    get => testString;

    set
    {
        testString = value;
        if (PropertyChanged != null)
        {
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(nameof(TestString)));
        }
    }
}

public MainWindow()
{
    this.InitializeComponent();
    UISettings uISettings = new UISettings();

    uISettings.ColorValuesChanged += UISettings_ColorValuesChanged;
}

private void myButton_Click(object sender, RoutedEventArgs e)
{
}

private void UISettings_ColorValuesChanged(UISettings sender, object args)
{
    int value = GetRegistryValue(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize", "AppsUseLightTheme");

    dispatcherQueue.TryEnqueue(() =>
    {
        TestString = "当前主题是" + Convert.ToString(value);
    });
}

private static int GetRegistryValue(string path, string paramName)
{
    RegistryKey root = Registry.CurrentUser;
    RegistryKey rk = root.OpenSubKey(path);

    return Convert.ToInt32(rk.GetValue(paramName, null));
}

public event PropertyChangedEventHandler PropertyChanged;

}

深色主题时,0 image 浅色主题时,1 image

Gaoyifei1011 avatar Nov 05 '22 12:11 Gaoyifei1011

Another quick workaround would be to use UISettings.ColorValuesChanged Event and Application.Current.RequestedTheme. Changing the system theme mode from Light to Dark or vice versa will trigger the ColorValuesChanged Event in real-time. This is confirmed to be working as expected in Windows App SDK 1.2.230118.102, packaged or unpackaged.

public MainWindow()
{
	this.InitializeComponent();

	UISettings = new();
	UISettings.ColorValuesChanged += UISettings_ColorValuesChanged;
}
private void UISettings_ColorValuesChanged(UISettings sender, object args)
{
	DispatcherQueue.TryEnqueue(Microsoft.UI.Dispatching.DispatcherQueuePriority.High,
		() =>
		{
			if(Application.Current.RequestedTheme == ApplicationTheme.Light)
			{
				SomeXAMLTextBlock.Text = "Light!";
			}
			else
			{
				SomeXAMLTextBlock.Text = "Dark!";
			}
		});
}

image image

DJRM2021 avatar Feb 12 '23 19:02 DJRM2021

It seems like UISettings.ColorValueChanged is not called on Windows 10 (at least for 19045) with Winui3 1.4.2. Repro here

HO-COOH avatar Nov 23 '23 07:11 HO-COOH