Repository navigation
Description
Current behavior
Subscribing to UISettings.ColorValuesChanged (or AccessibilitySettings.HighContrastChanged) never starts system theme observation. Both are plain field-like events, so adding a handler doesn't call SystemThemeHelper.ObserveThemeChanges().
The event only fires today because Application.InitializationCompleted subscribes to SystemThemeHelper.SystemThemeChanged / HighContrastChanged, and those events have explicit add accessors that call ObserveThemeChanges(). Without an initialized Application (class libraries, tests, or any code running before app init), the OS-side observation is never started and the event never fires.
Expected behavior
Adding a ColorValuesChanged handler should be enough for it to be raised when the OS theme or high contrast settings change, like it is on WinUI (where UISettings doesn't depend on an app being initialized).
This expectation is based on the WinUI API contract, not on a native WinUI repro or the WinUI C++ sources.
How to reproduce it
Confirmed by code inspection on master (cc4854160), not at runtime (toggling the OS theme from a test isn't practical):
UISettings.ColorValuesChangedis a plain event, with noaddaccessor: UISettings.cs#L58. The same applies to AccessibilitySettings.HighContrastChanged.- It is only raised from
SystemThemeHelper.NotifyHighContrastSettingsChanged()(SystemThemeHelper.cs#L302-L307) and from Application.cs#L468. - The OS observation is started only by
ObserveThemeChanges()(SystemThemeHelper.cs#L231), which is called from theaddaccessors ofSystemThemeHelper.SystemThemeChanged/HighContrastChanged(#L170-L190). The only subscriber in the framework is Application.InitializationCompleted.
So, in code that runs without Application.InitializationCompleted:
var settings = new UISettings();
settings.ColorValuesChanged += (s, e) => Console.WriteLine("changed");
// Switch the OS between light and dark: the handler is never called.Workaround
Make sure the app is initialized (this is always the case in a normal Uno app), or subscribe to something that goes through SystemThemeHelper first.
Works on UWP/WinUI
Yes (expected behavior).
Environment
Uno Platform master
NuGet package version(s)
master @ cc48541
Affected platforms
All heads (the code is shared in Uno.WinRT). Impact is low: in a real app Application always subscribes during init.
IDE version
N/A
Relevant plugins
No response
Anything else we need to know?
Suggested fix: use explicit add/remove accessors over a backing delegate for both events, with add calling SystemThemeHelper.ObserveThemeChanges() (it's idempotent), the same pattern SystemThemeHelper.SystemThemeChanged already uses. A runtime test can subscribe without an initialized app and assert the observation is started.
Activity