From ac8e8db72bf4a607b5518068ffb5e329b47e7f44 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Tue, 25 Feb 2020 16:00:33 +0100 Subject: [PATCH] Make Application handle style resources properly. --- src/Avalonia.Controls/Application.cs | 38 ++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Controls/Application.cs b/src/Avalonia.Controls/Application.cs index c49f7c3d09..0ae615fe23 100644 --- a/src/Avalonia.Controls/Application.cs +++ b/src/Avalonia.Controls/Application.cs @@ -44,6 +44,7 @@ namespace Avalonia private readonly Styler _styler = new Styler(); private Styles _styles; private IResourceDictionary _resources; + private bool _notifyingResourcesChanged; /// /// Defines the property. @@ -160,7 +161,20 @@ namespace Avalonia /// /// Global styles apply to all windows in the application. /// - public Styles Styles => _styles ?? (_styles = new Styles()); + public Styles Styles + { + get + { + if (_styles == null) + { + _styles = new Styles(); + ((ISetResourceParent)_styles).SetParent(this); + _styles.ResourcesChanged += ThisResourcesChanged; + } + + return _styles; + } + } /// bool IDataTemplateHost.IsDataTemplatesInitialized => _dataTemplates != null; @@ -233,9 +247,29 @@ namespace Avalonia } + private void NotifyResourcesChanged(ResourcesChangedEventArgs e) + { + if (_notifyingResourcesChanged) + { + return; + } + + try + { + _notifyingResourcesChanged = true; + (_resources as ISetResourceParent)?.ParentResourcesChanged(e); + (_styles as ISetResourceParent)?.ParentResourcesChanged(e); + ResourcesChanged?.Invoke(this, new ResourcesChangedEventArgs()); + } + finally + { + _notifyingResourcesChanged = false; + } + } + private void ThisResourcesChanged(object sender, ResourcesChangedEventArgs e) { - ResourcesChanged?.Invoke(this, e); + NotifyResourcesChanged(e); } private string _name;