Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ThemeListener.ThemeChanged does not work in WinUI 3 #555

Open
6 of 21 tasks
yoshiask opened this issue Dec 3, 2021 · 9 comments
Open
6 of 21 tasks

ThemeListener.ThemeChanged does not work in WinUI 3 #555

yoshiask opened this issue Dec 3, 2021 · 9 comments
Labels
bug Something isn't working regression What was working is now broke

Comments

@yoshiask
Copy link

yoshiask commented Dec 3, 2021

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.

  • 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

  • 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)
  • 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)
  • Insider Build (22000)

Device form factor:

  • Desktop
  • Xbox
  • Surface Hub
  • IoT

Visual Studio version:

  • 2017 (15.{minor_version})
  • 2019 (16.{minor_version})
  • 2022 (17.0.2)
@ghost
Copy link

ghost commented Dec 3, 2021

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
Copy link
Member

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
Copy link

azchohfi commented Dec 3, 2021

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
Copy link
Member

@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
Copy link

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

<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
Copy link

HO-COOH commented Nov 23, 2023

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

@Euclidite
Copy link

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

@HO-COOH
Copy link

HO-COOH commented Oct 30, 2024

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 Arlodotexe transferred this issue from CommunityToolkit/WindowsCommunityToolkit Nov 13, 2024
@Arlodotexe Arlodotexe added bug Something isn't working regression What was working is now broke labels Nov 13, 2024
@Arlodotexe
Copy link
Member

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! 🤞

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working regression What was working is now broke
Projects
Status: 🏗 In progress
Development

No branches or pull requests

8 participants
@azchohfi @yoshiask @Arlodotexe @michael-hawker @Euclidite @HO-COOH @Gaoyifei1011 and others