【问题标题】:WPF XAML Windows 11 Accent ColorWPF XAML Windows 11 强调色
【发布时间】:2021-12-17 05:43:11
【问题描述】:

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

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

我的代码:

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

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

谢谢。

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

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

    根据您的目标是 .NET 5 还是更早版本,您应该将 TargetFramework 设置为 net5.0-windows10.0.* 或将 Microsoft.Windows.SDK.Contracts NuGet 包安装为描述了here

    然后您可以使用 API 的返回值创建 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));
    

    【讨论】:

    • 非常感谢您的帮助。我添加了NuGet包并编辑了Target Framework,但是VS没有识别Windows.UI。我做错了什么?
    • 我正在使用.NET 6,并且所有 Windows 11 功能都运行良好。我尝试将net6.0-windows10.0.19041.0net5.0-windows10.0.19041.0 作为Target Framework,但两次尝试都失败了。
    【解决方案2】:

    Windows 10 中有效,在Windows 11 中也有效。

    操作系统:Windows 11 Build 10.0.22000.282

    VS: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 的情况下工作。

    【讨论】:

      猜你喜欢
      • 2016-05-27
      • 2021-02-06
      • 2016-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-28
      相关资源
      最近更新 更多