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 TargetType simplification bug #21764

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Controls/src/Build.Tasks/XamlCTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ bool TryCoreCompile(MethodDefinition initComp, ILRootNode rootnode, string xamlF
rootnode.Accept(new CreateObjectVisitor(visitorContext), null);
rootnode.Accept(new SetNamescopesAndRegisterNamesVisitor(visitorContext), null);
rootnode.Accept(new SetFieldVisitor(visitorContext), null);
rootnode.Accept(new SetResourcesVisitor(visitorContext), null);
rootnode.Accept(new SimplifyTypeExtensionVisitor(), null);
rootnode.Accept(new SetResourcesVisitor(visitorContext), null);
rootnode.Accept(new SetPropertiesVisitor(visitorContext, true), null);

il.Emit(Ret);
Expand Down
4 changes: 2 additions & 2 deletions src/Controls/src/Xaml/XamlLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ public static IResourceDictionary LoadResources(string xaml, IResourcesProvider
resources.Accept(new NamescopingVisitor(visitorContext), null); //set namescopes for {x:Reference}
resources.Accept(new CreateValuesVisitor(visitorContext), null);
resources.Accept(new RegisterXNamesVisitor(visitorContext), null);
resources.Accept(new FillResourceDictionariesVisitor(visitorContext), null);
resources.Accept(new SimplifyTypeExtensionVisitor(), null);
resources.Accept(new FillResourceDictionariesVisitor(visitorContext), null);
resources.Accept(new ApplyPropertiesVisitor(visitorContext, true), null);

return visitorContext.Values[resources] as IResourceDictionary;
Expand All @@ -207,8 +207,8 @@ static void Visit(RootNode rootnode, HydrationContext visitorContext, bool useDe
rootnode.Accept(new NamescopingVisitor(visitorContext), null); //set namescopes for {x:Reference}
rootnode.Accept(new CreateValuesVisitor(visitorContext), null);
rootnode.Accept(new RegisterXNamesVisitor(visitorContext), null);
rootnode.Accept(new FillResourceDictionariesVisitor(visitorContext), null);
rootnode.Accept(new SimplifyTypeExtensionVisitor(), null);
rootnode.Accept(new FillResourceDictionariesVisitor(visitorContext), null);
rootnode.Accept(new ApplyPropertiesVisitor(visitorContext, true), null);
}

Expand Down
12 changes: 12 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui21757.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui21757">
<Color x:Key="Gray-200">#C8C8C8</Color>
<Style x:Key="A" TargetType="BoxView">
<Setter Property="Color" Value="{StaticResource Gray-200}" />
</Style>
<Style x:Key="B" TargetType="{x:Type BoxView}">
<Setter Property="Color" Value="{StaticResource Gray-200}" />
</Style>
</ResourceDictionary>
60 changes: 60 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Maui21757.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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 partial class Maui21757
{
public Maui21757()
{
InitializeComponent();
}

public Maui21757(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 TypeLiteralAndXTypeCanBeUsedInterchangeably([Values(false, true)] bool useCompiledXaml)
{
var resourceDictionary = new Maui21757(useCompiledXaml);

var styleA = resourceDictionary["A"] as Style;
Assert.NotNull(styleA);
Assert.That(styleA.TargetType, Is.EqualTo(typeof(BoxView)));
Assert.That(styleA.Setters[0].Property, Is.EqualTo(BoxView.ColorProperty));
Assert.That(styleA.Setters[0].Value, Is.EqualTo(Color.FromArgb("#C8C8C8")));

var styleB = resourceDictionary["B"] as Style;
Assert.NotNull(styleB);
Assert.That(styleB.TargetType, Is.EqualTo(typeof(BoxView)));
Assert.That(styleB.Setters[0].Property, Is.EqualTo(BoxView.ColorProperty));
Assert.That(styleB.Setters[0].Value, Is.EqualTo(Color.FromArgb("#C8C8C8")));
}
}
}