From f39eaf9378dc1da0ef149201693da894d1193cb9 Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Fri, 9 Feb 2024 11:56:15 +0100 Subject: [PATCH] [X] Fix XamlCompilation of RD subclasses Fix an issue when compiling a non-direct descendant of RD - fixes #19535 --- .../src/Build.Tasks/SetPropertiesVisitor.cs | 2 +- .../Xaml.UnitTests/Issues/Maui19535.xaml | 11 ++++ .../Xaml.UnitTests/Issues/Maui19535.xaml.cs | 59 +++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/Controls/tests/Xaml.UnitTests/Issues/Maui19535.xaml create mode 100644 src/Controls/tests/Xaml.UnitTests/Issues/Maui19535.xaml.cs diff --git a/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs b/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs index 26e183912c10..20bde9bd9a47 100644 --- a/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs +++ b/src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs @@ -1464,7 +1464,7 @@ static bool CanAdd(VariableDefinition parent, XmlName propertyName, INode valueN static bool CanAddToResourceDictionary(VariableDefinition parent, TypeReference collectionType, IElementNode node, IXmlLineInfo lineInfo, ILContext context) { if (collectionType.FullName != "Microsoft.Maui.Controls.ResourceDictionary" - && collectionType.ResolveCached(context.Cache).BaseType?.FullName != "Microsoft.Maui.Controls.ResourceDictionary") + && !collectionType.InheritsFromOrImplements(context.Cache, context.Module.ImportReference(context.Cache, ("Microsoft.Maui.Controls", "Microsoft.Maui.Controls", "ResourceDictionary")))) return false; if (node.Properties.ContainsKey(XmlName.xKey)) diff --git a/src/Controls/tests/Xaml.UnitTests/Issues/Maui19535.xaml b/src/Controls/tests/Xaml.UnitTests/Issues/Maui19535.xaml new file mode 100644 index 000000000000..deba891bbeea --- /dev/null +++ b/src/Controls/tests/Xaml.UnitTests/Issues/Maui19535.xaml @@ -0,0 +1,11 @@ + + + + LightTheme + #6750A4 + #FFFBFE + + \ No newline at end of file diff --git a/src/Controls/tests/Xaml.UnitTests/Issues/Maui19535.xaml.cs b/src/Controls/tests/Xaml.UnitTests/Issues/Maui19535.xaml.cs new file mode 100644 index 000000000000..023ebf129a66 --- /dev/null +++ b/src/Controls/tests/Xaml.UnitTests/Issues/Maui19535.xaml.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Globalization; +using System.Linq; +using Microsoft.Maui.ApplicationModel; +using Microsoft.Maui.Controls.Core.UnitTests; +using Microsoft.Maui.Controls.Shapes; +using Microsoft.Maui.Devices; +using Microsoft.Maui.Dispatching; + +using Microsoft.Maui.Graphics; +using Microsoft.Maui.UnitTests; +using NUnit.Framework; + +namespace Microsoft.Maui.Controls.Xaml.UnitTests; + +public class Maui19535CustomThemeDictionary : ResourceDictionary +{ + +} + +public partial class Maui19535 : Maui19535CustomThemeDictionary +{ + public Maui19535() + { + InitializeComponent(); + } + + public Maui19535(bool useCompiledXaml) + { + //this stub will be replaced at compile time + } + + [TestFixture] + class Test + { + [SetUp] + public void Setup() + { + Application.SetCurrentApplication(new MockApplication()); + DispatcherProvider.SetCurrent(new DispatcherProviderStub()); + } + + [TearDown] public void TearDown() => AppInfo.SetCurrent(null); + + [Test] + public void SubClassOfRDShouldNotThrow([Values(false, true)] bool useCompiledXaml) + { + if (useCompiledXaml) + MockCompiler.Compile(typeof(Maui19535)); + var rd = new Maui19535(useCompiledXaml); + Assert.That(rd.Count, Is.EqualTo(3)); + Assert.True(rd.TryGetValue("CustomTheme", out var theme)); + Assert.That(theme, Is.EqualTo("LightTheme")); + } + } + +} \ No newline at end of file