CommunityToolkit / Windows

Collection of controls for WinUI 2, WinUI 3, and Uno Platform developers. Simplifies and demonstrates common developer tasks building experiences for Windows with .NET.
https://aka.ms/windowstoolkitdocs
Other
578 stars 70 forks source link

ThemeListener.ThemeChanged does not work in WinUI 3 #555

Open yoshiask opened 2 years ago

yoshiask commented 2 years ago

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.

Steps to Reproduce

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:

App min and target version:

Device form factor:

Visual Studio version:

ghost commented 2 years ago

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 🙌

michael-hawker commented 2 years ago

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...

azchohfi commented 2 years ago

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.

michael-hawker commented 2 years ago

@azchohfi @zateutsch this is another case similar to CommunityToolkit/WindowsCommunityToolkit#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?

Gaoyifei1011 commented 2 years ago

这是我的解决方案,可能有些笨拙。欢迎大佬献策。 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

HO-COOH commented 11 months ago

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

Euclidite commented 2 weeks ago

Any word on when this might be fixed, or if there's a known viable workaround?

HO-COOH commented 2 weeks ago

Any word on when this might be fixed, or if there's a known viable workaround?

There is, but somehow still not getting reviewed

Arlodotexe commented 1 day ago

Any word on when this might be fixed, or if there's a known viable workaround?

There is, but somehow still not getting reviewed

I've triaged and bumped the participants, hope to see movement again soon! 🤞