C# 检测强调色更改 WinRT XAML

C# Detect Accent Colour Changes WinRT XAML

我正在尝试检测 Application.Resources 资源字典中的更改,因此我可以在更新时自动将标题栏更改为强调色。所有 XAML 控件和元素都会自动更改,当将纯色画笔设置到 DSDFS 画笔的地址时,其内部值会发生变化。

这是我尝试用来检测变化的代码:

public static DependencyProperty accent = DependencyProperty.Register("DictChange", typeof(ResourceDictionary), typeof(Shell), new PropertyMetadata(Application.Current.Resources, new PropertyChangedCallback(accent_PropertyChanged)));

public ResourceDictionary DictChange
{
    get { return (ResourceDictionary)GetValue(accent); }
    set { SetValue(accent, value); }
}

private static void accent_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    _app.SetTitlebar();
}

虽然我假设它是错误的,或者我不确定这是否是检测变化的正确做法。在之前的迭代中,我使用了 Application.Current.Resources["SystemControlBackgroundAccentBrush"] as SolidColorBrush 并试图检测它的 属性,但这也没有用。

我做错了什么?请帮助:)

I'm assuming its wrong though, or I'm not sure if that is the right thing to do to detect changes.

您无法通过注册 DependencyProperty 来检测资源字典中的键值更改,因为 ResourceDictionay 不是 ObservableCollection Class

没有内置支持来检测资源字典中的键值变化。

作为解决方法,您可以考虑创建一个内部可观察集合来检测更改。

它可能没有错,但它可能不是可用的最佳解决方案。

在 WinRT XAML 中,我们有这个新的 ThemeResource 可以自动更新资源。棘手的一点是找到一种方法将 ApplicationView.GetForCurrentView().TitleBar.BackgroundColor 绑定到 SystemControlBackgroundAccentBrush.

my answer to this question 中,我创建了一个 Behavior,将 custom TitleBar 附加到页面。如果将 Background 属性 修改为这样的内容 -

<local:FullScreenModeTitleBarBehavior Background="{ThemeResource SystemControlBackgroundAccentBrush}" />

现在运行 app 你会看到当你改变系统的accent color 背景颜色会更新,如下图-

基本上在你的情况下,你只需要创建一个类似的(和更简单的?)Behavior,它就像一个通往 link BackgroundColorTitleBar 的桥梁] 到 SystemControlBackgroundAccentBrush,通过 ThemeResource 绑定。

希望对您有所帮助!