Skip to content

Commit 00879c8

Browse files
[SG] generated code behind for RD in global xmlns
we weren't generating the boiler plate for ResourceDictionary using the global xmlns for XamlC compilation or sourcegen. fixing that, and getting back the #performance - fies #31602
1 parent de63a9d commit 00879c8

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

src/Controls/src/SourceGen/CodeBehindCodeWriter.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,15 +312,29 @@ public static bool TryParseXaml(XamlProjectItemForCB parseResult, string uid, Co
312312
XmlnsHelper.ParseXmlns(rootClass.Value, out rootType, out rootClrNamespace, out _, out _);
313313
}
314314
#if _MAUIXAML_SOURCEGEN_BACKCOMPAT
315-
else if (hasXamlCompilationProcessingInstruction && root.NamespaceURI == XamlParser.MauiUri)
315+
else if (hasXamlCompilationProcessingInstruction
316+
&& (root.NamespaceURI == XamlParser.MauiUri || root.NamespaceURI == XamlParser.MauiGlobalUri))
317+
#else
318+
else if (root.NamespaceURI == XamlParser.MauiUri || root.NamespaceURI == XamlParser.MauiGlobalUri)
319+
#endif
316320
{
321+
//make sure the base type can be resolved. if not, don't consider this as xaml, and move away
322+
var typeArgs = GetAttributeValue(root, "TypeArguments", XamlParser.X2006Uri, XamlParser.X2009Uri);
323+
try
324+
{
325+
var basetype = new XmlType(root.NamespaceURI, root.LocalName, typeArgs != null ? TypeArgumentsParser.ParseExpression(typeArgs, nsmgr, null) : null).GetTypeSymbol(null, compilation, xmlnsCache);
326+
}
327+
catch
328+
{
329+
return false;
330+
}
331+
317332
rootClrNamespace = "__XamlGeneratedCode__";
318333
rootType = $"__Type{uid}";
319334
generateDefaultCtor = true;
320335
addXamlCompilationAttribute = true;
321336
hideFromIntellisense = true;
322337
}
323-
#endif
324338
else if (parseResult?.ProjectItem?.ManifestResourceName != null && parseResult.ProjectItem.TargetPath != null)
325339
{ // rootClass == null && !hasXamlCompilationProcessingInstruction) {
326340
xamlResourceIdOnly = true; //only generate the XamlResourceId assembly attribute

src/Controls/src/SourceGen/ISymbolExtensions.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
using System.Collections.Generic;
21
using System.Collections.Immutable;
3-
using System.ComponentModel;
42
using System.Linq;
53
using Microsoft.CodeAnalysis;
6-
using Microsoft.CodeAnalysis.CSharp;
7-
using Microsoft.CodeAnalysis.CSharp.Syntax;
8-
using Microsoft.CodeAnalysis.Text;
94

105
namespace Microsoft.Maui.Controls.SourceGen;
116

src/Controls/tests/SourceGen.UnitTests/SourceGenXamlCodeBehindTests.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,35 @@ public void TestCodeBehindGenerator_AggregatedXmlns()
9292
Assert.IsTrue(generated.Contains("global::Microsoft.Maui.Controls.Label label", StringComparison.Ordinal));
9393
}
9494

95+
[Test]
96+
public void TestCodeBehindGenerator_AggregatedXmlnsOnRD()
97+
{
98+
var xaml =
99+
"""
100+
<?xml version="1.0" encoding="UTF-8"?>
101+
<ResourceDictionary
102+
xmlns="http://schemas.microsoft.com/dotnet/maui/global"
103+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
104+
<x:String x:Key="MyString">Hello MAUI!</x:String>
105+
</ResourceDictionary>
106+
""";
107+
108+
var code =
109+
"""
110+
using Microsoft.Maui.Controls;
111+
[assembly: XmlnsDefinition("http://schemas.microsoft.com/dotnet/maui/global", "http://schemas.microsoft.com/dotnet/2021/maui")]
112+
""";
113+
var compilation = CreateMauiCompilation();
114+
compilation = compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(code));
115+
var result = RunGenerator<CodeBehindGenerator>(compilation, new AdditionalXamlFile("Test.xaml", xaml));
116+
117+
Assert.IsFalse(result.Diagnostics.Any());
118+
119+
var generated = result.Results.Single().GeneratedSources.Single(gs => gs.HintName.EndsWith(".sg.cs")).SourceText.ToString();
120+
121+
Assert.IsTrue(generated.Contains("public partial class __Type", StringComparison.Ordinal));
122+
}
123+
95124
public void TestCodeBehindGenerator_LocalXaml([Values] bool resolvedType)
96125
{
97126
var xaml =
@@ -305,9 +334,11 @@ public void TestCodeBehindGenerator_NotXaml()
305334
</foo>
306335
""";
307336
var compilation = SourceGeneratorDriver.CreateMauiCompilation();
337+
compilation = compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText("[assembly: global::Microsoft.Maui.Controls.Xaml.Internals.AllowImplicitXmlnsDeclaration]"));
308338
var result = SourceGeneratorDriver.RunGenerator<CodeBehindGenerator>(compilation, new AdditionalXamlFile("Test.xaml", xaml));
309339

310-
Assert.That(result.Diagnostics.Any());
340+
var generated = result.Results.Single().GeneratedSources.Single(gs => gs.HintName.EndsWith(".sg.cs")).SourceText.ToString();
341+
Assert.That(result.Diagnostics.Any() || string.IsNullOrWhiteSpace(generated));
311342
}
312343

313344
[Test]

0 commit comments

Comments
 (0)