Skip to content

Proposal: Non-XAML ThemeResource #10029

Open

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:

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 or BorderBrush 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:

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);
}
, but these are very inefficient and definitely not ideal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    feature proposalNew feature proposalneeds-triageIssue needs to be triaged by the area owners

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions