From dbb7828e75fe2406729265b8c5410546dfcca9ff Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Thu, 12 Nov 2020 11:49:53 +0100 Subject: [PATCH] Don't subscribe to inner observable if not active. When subscribing to a `PropertySetterBindingInstance`, if the owner style is not active then there's no need to subscribe to the inner observable as this can cause resource lookups etc. Part of fixing #5027. --- .../Styling/PropertySetterBindingInstance.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Avalonia.Styling/Styling/PropertySetterBindingInstance.cs b/src/Avalonia.Styling/Styling/PropertySetterBindingInstance.cs index 929f7142bb5d..a67190c83163 100644 --- a/src/Avalonia.Styling/Styling/PropertySetterBindingInstance.cs +++ b/src/Avalonia.Styling/Styling/PropertySetterBindingInstance.cs @@ -92,6 +92,7 @@ public void Activate() { if (!_isActive) { + _innerSubscription ??= _binding.Observable.Subscribe(_inner); _isActive = true; PublishNext(); } @@ -102,6 +103,8 @@ public void Deactivate() if (_isActive) { _isActive = false; + _innerSubscription?.Dispose(); + _innerSubscription = null; PublishNext(); } } @@ -148,7 +151,10 @@ void IObserver>.OnNext(BindingValue value) protected override void Subscribed() { - _innerSubscription = _binding.Observable.Subscribe(_inner); + if (_isActive) + { + _innerSubscription = _binding.Observable.Subscribe(_inner); + } } protected override void Unsubscribed()