Changing the value of ThemeResource at run time is not reflected in other views

I use a custom theme dictionary in my UWP application. I am changing the value of ThemeResource at runtime. This change is reflected only in the main, and not in other representations. Even if I create a new view after changing the value of the resource, the new view uses only the internal value of the resource. Is there something I'm doing wrong?

This is how I change the value of a resource.

(Application.Current.Resources["BackgroundBrush"] as SolidColorBrush).Color = Windows.UI.Colors.Black;

My second view of XAML:

<Grid Background="{ThemeResource BackgroundBrush}"/>

Even my main view has the same XAML.

Here is the complete project. Download Repo as zip

+4
source share
2

, . , . . Application.Resources, , .

Application.Resources - ResourceDictionary ResourceDictionary DependencyObject, Application.Resources DependencyObject.

DependencyObject , Window . , :

. DependencyObject threading DependencyObject.

, Window Application.Resources. Application.Resources ResourceDictionary. BackgroundBrush ,

(Application.Current.Resources["BackgroundBrush"] as SolidColorBrush).Color = Windows.UI.Colors.Black;

Application.Current.Resources, Window.

, , , , , . , BackgroundBrushColor App, , :

private void ThemeChanger_Click(object sender, RoutedEventArgs e)
{
    App.BackgroundBrushColor = Windows.UI.Color.FromArgb(Convert.ToByte(random.Next(255)), Convert.ToByte(random.Next(255)), Convert.ToByte(random.Next(255)), Convert.ToByte(random.Next(255)));

    (Application.Current.Resources["BackgroundBrush"] as SolidColorBrush).Color = App.BackgroundBrushColor;
}

private async Task CreateNewViewAsync()
{
    CoreApplicationView newView = CoreApplication.CreateNewView();
    int newViewId = 0;

    await newView.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        (Application.Current.Resources["BackgroundBrush"] as SolidColorBrush).Color = App.BackgroundBrushColor;

        Frame frame = new Frame();
        frame.Navigate(typeof(SecondaryPage), null);
        Window.Current.Content = frame;
        // You have to activate the window in order to show it later.
        Window.Current.Activate();

        newViewId = ApplicationView.GetForCurrentView().Id;
    });
    bool viewShown = await ApplicationViewSwitcher.TryShowAsStandaloneAsync(newViewId);
}

, . ThemeChanged SecondaryPage.xaml.cs , , . .

, ThemeChanged , , ThemeManager_ThemeChanged, SecondaryPage.xaml.cs, , Application.Current.Resources in ThemeManager_ThemeChanged, - ResourceDictionary, . . , "" .

, this.Dispatcher.RunAsync , :

private async void ThemeManager_ThemeChanged(Utils.ThemeManager theme)
{
    await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        ((SolidColorBrush)Application.Current.Resources["BackgroundBrush"]).Color = theme.HighAccentColorBrush.Color;
    });
}

, : " , ." , SolidColorBrush DependencyObject. , HighAccentColorBrush SolidColorBrush Color, :

private async void ThemeManager_ThemeChanged(Utils.ThemeManager theme)
{
    await this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
        ((SolidColorBrush)Application.Current.Resources["BackgroundBrush"]).Color = theme.HighAccentColorBrush;
    });
}
+8

.

    void OnClicked(object sender, System.EventArgs e)
    {
        var btn = sender as Button;

        MessagingCenter.Send(this, "ThemeButtonClicked", btn.BackgroundColor);

        Application.Current.Properties["Theme"] = btn.BackgroundColor;
        Application.Current.SavePropertiesAsync();

        Navigation.PopAsync(true);

    }

, .

 MessagingCenter.Subscribe<Theme, Color>(this, "ThemeButtonClicked", OnThemeChanged);

, , .

  private void OnThemeChanged(Theme source, Color cl)
    {
        this.BackgroundColor = cl;
        layout.BackgroundColor = cl;

    }

. , .

0

Source: https://habr.com/ru/post/1674918/


All Articles