WPF XAML Windows 11 强调色

WPF XAML Windows 11 Accent Color

有没有一种简单的方法可以在 XAML 中获取 Windows 10 / Windows 11 强调色?

我看了很多例子,但大部分都不行。我还看到了一些模仿 C# 中颜色变化行为的示例,但它们在 Windows 11 中不起作用。

我的代码:

<SolidColorBrush x:Key="{x:Static SystemColors.WindowBrushKey}"
    Color="{x:Static SystemParameters.WindowGlassBrush}"/>

我只想将 Color 设置为 windows 当前强调色。

谢谢。

您应该能够使用 UISettings.GetColorValue WinRT API 检索强调色。

根据您的目标是 .NET 5 还是更早的版本,您应该将 TargetFramework 设置为 net5.0-windows10.0.* 或按照 here.

所述安装 Microsoft.Windows.SDK.Contracts NuGet 包

然后您可以使用 API 的 return 值创建一个 SolidColorBrush:

var color = new Windows.UI.ViewManagement.UISettings()
    .GetColorValue(UIColorType.Accent);
var brush = new SolidColorBrush
    (Color.FromArgb(color.A, color.R, color.G, color.B));

Windows 10 中有效的方法在 Windows 11 中也有效。

OS: Windows 11 Build 10.0.22000.282

对比:2022 Preview 7.0

WPF: .NET 6

UserLabel.Background = SystemParameters.WindowGlassBrush;
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource TextBase}"
       x:Key="TextColored">
    <Setter Property="FontWeight"
            Value="SemiBold"/>
    <Setter Property="Foreground"
            Value="{x:Static SystemParameters.WindowGlassBrush}"/>
</Style>

这没有添加任何 NuGets