Skip to content

Commit f39eaf9

Browse files
[X] Fix XamlCompilation of RD subclasses
Fix an issue when compiling a non-direct descendant of RD - fixes #19535
1 parent b43f265 commit f39eaf9

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1464,7 +1464,7 @@ static bool CanAdd(VariableDefinition parent, XmlName propertyName, INode valueN
14641464
static bool CanAddToResourceDictionary(VariableDefinition parent, TypeReference collectionType, IElementNode node, IXmlLineInfo lineInfo, ILContext context)
14651465
{
14661466
if (collectionType.FullName != "Microsoft.Maui.Controls.ResourceDictionary"
1467-
&& collectionType.ResolveCached(context.Cache).BaseType?.FullName != "Microsoft.Maui.Controls.ResourceDictionary")
1467+
&& !collectionType.InheritsFromOrImplements(context.Cache, context.Module.ImportReference(context.Cache, ("Microsoft.Maui.Controls", "Microsoft.Maui.Controls", "ResourceDictionary"))))
14681468
return false;
14691469

14701470
if (node.Properties.ContainsKey(XmlName.xKey))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<local:Maui19535CustomThemeDictionary xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests"
5+
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui19535">
6+
7+
<x:String x:Key="CustomTheme">LightTheme</x:String>
8+
<Color x:Key="PrimaryBackground">#6750A4</Color>
9+
<Color x:Key="ContentBackground">#FFFBFE</Color>
10+
11+
</local:Maui19535CustomThemeDictionary>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Globalization;
5+
using System.Linq;
6+
using Microsoft.Maui.ApplicationModel;
7+
using Microsoft.Maui.Controls.Core.UnitTests;
8+
using Microsoft.Maui.Controls.Shapes;
9+
using Microsoft.Maui.Devices;
10+
using Microsoft.Maui.Dispatching;
11+
12+
using Microsoft.Maui.Graphics;
13+
using Microsoft.Maui.UnitTests;
14+
using NUnit.Framework;
15+
16+
namespace Microsoft.Maui.Controls.Xaml.UnitTests;
17+
18+
public class Maui19535CustomThemeDictionary : ResourceDictionary
19+
{
20+
21+
}
22+
23+
public partial class Maui19535 : Maui19535CustomThemeDictionary
24+
{
25+
public Maui19535()
26+
{
27+
InitializeComponent();
28+
}
29+
30+
public Maui19535(bool useCompiledXaml)
31+
{
32+
//this stub will be replaced at compile time
33+
}
34+
35+
[TestFixture]
36+
class Test
37+
{
38+
[SetUp]
39+
public void Setup()
40+
{
41+
Application.SetCurrentApplication(new MockApplication());
42+
DispatcherProvider.SetCurrent(new DispatcherProviderStub());
43+
}
44+
45+
[TearDown] public void TearDown() => AppInfo.SetCurrent(null);
46+
47+
[Test]
48+
public void SubClassOfRDShouldNotThrow([Values(false, true)] bool useCompiledXaml)
49+
{
50+
if (useCompiledXaml)
51+
MockCompiler.Compile(typeof(Maui19535));
52+
var rd = new Maui19535(useCompiledXaml);
53+
Assert.That(rd.Count, Is.EqualTo(3));
54+
Assert.True(rd.TryGetValue("CustomTheme", out var theme));
55+
Assert.That(theme, Is.EqualTo("LightTheme"));
56+
}
57+
}
58+
59+
}

0 commit comments

Comments
 (0)