Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[X] Fix XamlCompilation of RD subclasses #20463

Merged
merged 1 commit into from Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Controls/src/Build.Tasks/SetPropertiesVisitor.cs
Expand Up @@ -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))
Expand Down
11 changes: 11 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui19535.xaml
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<local:Maui19535CustomThemeDictionary xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui19535">

<x:String x:Key="CustomTheme">LightTheme</x:String>
<Color x:Key="PrimaryBackground">#6750A4</Color>
<Color x:Key="ContentBackground">#FFFBFE</Color>

</local:Maui19535CustomThemeDictionary>
59 changes: 59 additions & 0 deletions 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"));
}
}

}