C# 检测强调色更改 WinRT XAML

标签 c# xaml windows-runtime windows-10 uwp

我正在尝试检测 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"] 用作 SolidColorBrush 并尝试检测其属性,但这也不起作用。

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

最佳答案

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

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

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

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

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

enter image description here

基本上在你的情况下,你只需要创建一个类似的(和更简单的?)行为,它就像一个桥梁来链接 TitleBar 的 BackgroundColor SystemControlBackgroundAccentBrush,通过 ThemeResource 绑定(bind)。

希望这对您有所帮助!

关于C# 检测强调色更改 WinRT XAML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32626470/

相关文章:

c# - Windows 8.1 中 MediaElement 的样式

c# - 从字符串中删除无法识别的 ASCII 字符

C# EF 在一个请求中更新实体

javascript - 尝试让一个 iframe 在另一个 iframe 之前加载

c# - Api Controller 可以工作,但 web razor Controller 不能使用 owin

c# - 使用按钮选择 Telerik 网格 - MVVM

c++ - 为 Windows 8.1 和 Windows Phone 8.1 创建单个 ARM 二进制文件

file-io - 在 Windows 8 RT 中替换或重新创建文件会保留旧的 DateCreated 值

wpf - 如何使用不同类型对象的二维数组进行数据绑定(bind)

c# - 如何在 ScrollViewer 中滚动水平 StackPanel?