From 35fb2bc6570970a0f98b3600ed389624e42ce003 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sat, 30 Mar 2024 01:07:08 +0100 Subject: [PATCH] Fix "BindingExpression has not been started" (#15170) * Added failing test for #14753. * Don't try to publish on non-running binding. This can happen in cases like #14653 where: 1. An event is raised with 2 binding expression subscribers 2. The first subscriber causes the 2nd subscriber to be stopped 3. The second subscriber is called from the event, even though it has been stopped (as the event list was cached at step 1) 4. It calls `PublishValue` causing an exception Easiest to just do nothing in `PublishValue` when this scenario happens. --- .../Data/Core/UntypedBindingExpressionBase.cs | 3 ++ .../DynamicResourceExtensionTests.cs | 36 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/Avalonia.Base/Data/Core/UntypedBindingExpressionBase.cs b/src/Avalonia.Base/Data/Core/UntypedBindingExpressionBase.cs index fc0135b199..20686c3546 100644 --- a/src/Avalonia.Base/Data/Core/UntypedBindingExpressionBase.cs +++ b/src/Avalonia.Base/Data/Core/UntypedBindingExpressionBase.cs @@ -403,6 +403,9 @@ public abstract class UntypedBindingExpressionBase : BindingExpressionBase, /// The new binding or data validation error. private protected void PublishValue(object? value, BindingError? error = null) { + if (!IsRunning) + return; + // When binding to DataContext and the expression results in a binding error, the binding // expression should produce null rather than UnsetValue in order to not propagate // incorrect DataContexts from parent controls while things are being set up. diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/DynamicResourceExtensionTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/DynamicResourceExtensionTests.cs index 9379f94edd..05b77b5325 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/DynamicResourceExtensionTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/DynamicResourceExtensionTests.cs @@ -924,6 +924,42 @@ namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions window.Content = null; } + [Fact] + public void Handles_Clearing_Resources_With_Dynamic_Theme_In_Dynamic_Template() + { + // Issue #14753 + using var app = UnitTestApplication.Start(TestServices.StyledWindow); + var xaml = """ + + + Blue + + + + + + + + + + + +