diff --git a/src/Controls/src/SourceGen/CodeBehindCodeWriter.cs b/src/Controls/src/SourceGen/CodeBehindCodeWriter.cs index 94292cee8001..f0db31a65627 100644 --- a/src/Controls/src/SourceGen/CodeBehindCodeWriter.cs +++ b/src/Controls/src/SourceGen/CodeBehindCodeWriter.cs @@ -312,15 +312,29 @@ public static bool TryParseXaml(XamlProjectItemForCB parseResult, string uid, Co XmlnsHelper.ParseXmlns(rootClass.Value, out rootType, out rootClrNamespace, out _, out _); } #if _MAUIXAML_SOURCEGEN_BACKCOMPAT - else if (hasXamlCompilationProcessingInstruction && root.NamespaceURI == XamlParser.MauiUri) + else if (hasXamlCompilationProcessingInstruction + && (root.NamespaceURI == XamlParser.MauiUri || root.NamespaceURI == XamlParser.MauiGlobalUri)) +#else + else if (root.NamespaceURI == XamlParser.MauiUri || root.NamespaceURI == XamlParser.MauiGlobalUri) +#endif { + //make sure the base type can be resolved. if not, don't consider this as xaml, and move away + var typeArgs = GetAttributeValue(root, "TypeArguments", XamlParser.X2006Uri, XamlParser.X2009Uri); + try + { + var basetype = new XmlType(root.NamespaceURI, root.LocalName, typeArgs != null ? TypeArgumentsParser.ParseExpression(typeArgs, nsmgr, null) : null).GetTypeSymbol(null, compilation, xmlnsCache); + } + catch + { + return false; + } + rootClrNamespace = "__XamlGeneratedCode__"; rootType = $"__Type{uid}"; generateDefaultCtor = true; addXamlCompilationAttribute = true; hideFromIntellisense = true; } -#endif else if (parseResult?.ProjectItem?.ManifestResourceName != null && parseResult.ProjectItem.TargetPath != null) { // rootClass == null && !hasXamlCompilationProcessingInstruction) { xamlResourceIdOnly = true; //only generate the XamlResourceId assembly attribute diff --git a/src/Controls/src/SourceGen/ISymbolExtensions.cs b/src/Controls/src/SourceGen/ISymbolExtensions.cs index aed48bfa5e58..4b51bcb72ec3 100644 --- a/src/Controls/src/SourceGen/ISymbolExtensions.cs +++ b/src/Controls/src/SourceGen/ISymbolExtensions.cs @@ -1,11 +1,6 @@ -using System.Collections.Generic; using System.Collections.Immutable; -using System.ComponentModel; using System.Linq; using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using Microsoft.CodeAnalysis.Text; namespace Microsoft.Maui.Controls.SourceGen; diff --git a/src/Controls/tests/SourceGen.UnitTests/SourceGenXamlCodeBehindTests.cs b/src/Controls/tests/SourceGen.UnitTests/SourceGenXamlCodeBehindTests.cs index e54a64be956c..c91c4bcddb40 100644 --- a/src/Controls/tests/SourceGen.UnitTests/SourceGenXamlCodeBehindTests.cs +++ b/src/Controls/tests/SourceGen.UnitTests/SourceGenXamlCodeBehindTests.cs @@ -92,6 +92,35 @@ public void TestCodeBehindGenerator_AggregatedXmlns() Assert.IsTrue(generated.Contains("global::Microsoft.Maui.Controls.Label label", StringComparison.Ordinal)); } + [Test] + public void TestCodeBehindGenerator_AggregatedXmlnsOnRD() + { + var xaml = +""" + + + Hello MAUI! + +"""; + + var code = + """ +using Microsoft.Maui.Controls; +[assembly: XmlnsDefinition("http://schemas.microsoft.com/dotnet/maui/global", "http://schemas.microsoft.com/dotnet/2021/maui")] +"""; + var compilation = CreateMauiCompilation(); + compilation = compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(code)); + var result = RunGenerator(compilation, new AdditionalXamlFile("Test.xaml", xaml)); + + Assert.IsFalse(result.Diagnostics.Any()); + + var generated = result.Results.Single().GeneratedSources.Single(gs => gs.HintName.EndsWith(".sg.cs")).SourceText.ToString(); + + Assert.IsTrue(generated.Contains("public partial class __Type", StringComparison.Ordinal)); + } + public void TestCodeBehindGenerator_LocalXaml([Values] bool resolvedType) { var xaml = @@ -305,9 +334,11 @@ public void TestCodeBehindGenerator_NotXaml() """; var compilation = SourceGeneratorDriver.CreateMauiCompilation(); + compilation = compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText("[assembly: global::Microsoft.Maui.Controls.Xaml.Internals.AllowImplicitXmlnsDeclaration]")); var result = SourceGeneratorDriver.RunGenerator(compilation, new AdditionalXamlFile("Test.xaml", xaml)); - Assert.That(result.Diagnostics.Any()); + var generated = result.Results.Single().GeneratedSources.Single(gs => gs.HintName.EndsWith(".sg.cs")).SourceText.ToString(); + Assert.That(result.Diagnostics.Any() || string.IsNullOrWhiteSpace(generated)); } [Test]