本文介绍了获取Windows 8自动颜色主题的活动颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Windows 8 中,我已将配色方案设置为自动并将我的墙纸配置为在 x 分钟后更改.配色方案根据活动壁纸而变化.

In Windows 8, I have set the color scheme to automatic and configured my wallpaper to change after x minutes. The color scheme changes according to the active wallpaper.

我正在开发一个 WPF 应用程序,并希望在 Windows 更改配色方案以匹配当前壁纸时更改我的渐变.

I'm developing a WPF application and would like to have my gradients change when Windows changes the color scheme to match the current wallpaper.

有没有办法获取当前/实际的配色方案并在 C# 中收到更改通知?

Is there a way get the current/actual color scheme and be notified of the change in C#?

推荐答案

这可以在 .NET 4.5 及更高版本中完成,无需 P/Invokes.SystemParameters 类现在具有静态 WindowGlassBrushWindowGlassColor 属性以及 StaticPropertyChanged 事件.

This can be done in .NET 4.5 and later without P/Invokes. The SystemParameters class now has static WindowGlassBrush and WindowGlassColor properties along with a StaticPropertyChanged event.

从 XAML,您可以绑定到 WindowGlassBrush 属性,例如:

From XAML, you can bind to the WindowGlassBrush property like:

<Grid Background="{x:Static SystemParameters.WindowGlassBrush}">

但是,通过此分配,当 Windows 更改其颜色时,背景颜色不会自动更新.不幸的是,SystemParameters 没有提供WindowGlassBrushKeyWindowGlassColorKey 属性作为 ResourceKeys 与 DynamicResource 一起使用,因此获取更改通知需要背后的代码来处理 StaticPropertyChanged 事件.

However, with this assignment the Background color won't get updated automatically when Windows changes its colors. Unfortunately, SystemParameters does not provide WindowGlassBrushKey or WindowGlassColorKey properties to use as ResourceKeys with DynamicResource, so getting change notifications requires code behind to handle the StaticPropertyChanged event.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        this.InitializeComponent();
        SystemParameters.StaticPropertyChanged += this.SystemParameters_StaticPropertyChanged;

        // Call this if you haven't set Background in XAML.
        this.SetBackgroundColor();
    }

    protected override void OnClosed(EventArgs e)
    {
        SystemParameters.StaticPropertyChanged -= this.SystemParameters_StaticPropertyChanged;
        base.OnClosed(e);
    }

    private void SetBackgroundColor()
    {
        this.Background = SystemParameters.WindowGlassBrush;
    }

    private void SystemParameters_StaticPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        if (e.PropertyName == "WindowGlassBrush")
        {
            this.SetBackgroundColor();
        }
    }
}

这篇关于获取Windows 8自动颜色主题的活动颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 14:45