C# Detect Accent Colour Changes WinRT XAML

2019-04-11 05:56发布

I am trying to detect changes in the Application.Resources Resource dictionary, so I can automatically change the Titlebar to the Accent Colour when it updates. All of the XAML controls and elements change automatically, and when setting a solid colour brush to the address of the DSDFS brush, its internal value changes.

This is the code I have tried to use to detect the change:

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();
}

I'm assuming its wrong though, or I'm not sure if that is the right thing to do to detect changes. There was a previous iteration where I used Application.Current.Resources["SystemControlBackgroundAccentBrush"] as SolidColorBrush and tried to detect its property, but that didn't work either.

What am I doing wrong? Please help :)

2条回答
Lonely孤独者°
2楼-- · 2019-04-11 06:32

It might not be wrong, but it's probably not the best solution available.

In WinRT XAML, we have this new ThemeResource that updates the resources automatically. The tricky bit is to find a way to bind the ApplicationView.GetForCurrentView().TitleBar.BackgroundColor to SystemControlBackgroundAccentBrush.

In my answer to this question, I created a Behavior that attaches a custom TitleBar to the page. If you modify the Background property to something like this -

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

Now run the app and you will see the background color gets updated when you change the accent color of the system, as shown in the picture below -

enter image description here

Basically in your case, you just need to create a similar (& simpler?) Behavior that acts like a bridge to link the BackgroundColor of the TitleBar to the SystemControlBackgroundAccentBrush, via ThemeResource binding.

Hope this helps!

查看更多
贼婆χ
3楼-- · 2019-04-11 06:41

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

You cannot detect the key-value changes in the Resource Dictionary by registering a DependencyProperty because the ResourceDictionay is not an ObservableCollection Class. enter image description here

There are no build-in support to detect the key-value changes in a Resource Dictionary.

As a workaround, you can consider create an internal observable collection to detect the changes.

查看更多
登录 后发表回答