Skip to content
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
18 changes: 16 additions & 2 deletions src/Controls/src/SourceGen/CodeBehindCodeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions src/Controls/src/SourceGen/ISymbolExtensions.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
"""
<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary
xmlns="http://schemas.microsoft.com/dotnet/maui/global"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<x:String x:Key="MyString">Hello MAUI!</x:String>
</ResourceDictionary>
""";

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<CodeBehindGenerator>(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 =
Expand Down Expand Up @@ -305,9 +334,11 @@ public void TestCodeBehindGenerator_NotXaml()
</foo>
""";
var compilation = SourceGeneratorDriver.CreateMauiCompilation();
compilation = compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText("[assembly: global::Microsoft.Maui.Controls.Xaml.Internals.AllowImplicitXmlnsDeclaration]"));
var result = SourceGeneratorDriver.RunGenerator<CodeBehindGenerator>(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]
Expand Down
Loading