-
Notifications
You must be signed in to change notification settings - Fork 769
Open
Labels
Description
Proposal: Non-XAML ThemeResource
Summary
Currently it is only possible to set ThemeResource
from XAML, which is problematic in various scenarios, for example in Uno Platform's C# Markup:
- [MVUX/CSM] Resources don't update after changing Fluent Theme manually unoplatform/uno.extensions#2429
- https://github.com/unoplatform/uno.csharpmarkup/issues/716
This is especially problemtai
Rationale
- It is not possible to retrieve a theme-dependent brush for a given theme from a
ResourceDictionary
in code - It is not possible to set a property like
Background
orBorderBrush
of a control to a theme-dependent brush in code
Scope
Capability | Priority |
---|---|
Retrieve a theme-dependent value of a resource from ResourceDictionary in code |
Must |
Set a control's dependency property to a theme-dependent resource in code | Must |
Workarounds
There some very limited ways to work around this, like:
microsoft-ui-xaml/src/controls/dev/WebView2/WebView2.cpp
Lines 909 to 927 in 66a7b0a
winrt::Color WebView2::GetThemeBackgroundColor() | |
{ | |
bool isHighContrast = GetAccessibilitySettings().HighContrast(); | |
std::wstring helperBrushName = isHighContrast ? L"BrushForThemeBackgroundColor_HC" : L"BrushForThemeBackgroundColor"; | |
// Ensure helper SCB referencing appropriate color as a ThemeResource is added to WV2.Resources. | |
// This is needed since programatic lookup of theme resources themselves does not account for active theme. | |
if (!this->Resources().HasKey(box_value(helperBrushName))) | |
{ | |
std::wstring resourceName = isHighContrast ? L"SystemColorWindowColor" : L"SolidBackgroundFillColorBase"; | |
winrt::SolidColorBrush helperBrush = winrt::XamlReader::Load( | |
L"<SolidColorBrush xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' " | |
L"xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' " | |
L"x:Name='" + helperBrushName + L"' " | |
L"Color='{ThemeResource " + resourceName + L"}'/>" | |
).as<winrt::SolidColorBrush>(); | |
this->Resources().Insert(box_value(helperBrushName), helperBrush); | |
} |
whiskhub, ghost1372, driver1998, Xiaoy312, qian-o and 2 more