Skip to content

Commit

Permalink
[X] Change the default value for xaml-comp (#19390)
Browse files Browse the repository at this point in the history
The default value for the xaml-comp processing instruction used to be
false, meaning the a XAML ResourceDictionary without code-behind wasn't
compiled by XamlC unless the `<?xaml-comp compile="true" ?>` processing
instruction was specified.

From now on, we will compile all XAML-only RD, unless the 
`<?xaml-comp compile="false" ?>` is set.

Thsi will help with performance and trimming
  • Loading branch information
StephaneDelcroix authored Dec 19, 2023
1 parent f975af2 commit 6cb9048
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/Controls/src/SourceGen/CodeBehindGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ static bool TryParseXaml(SourceText text, string uid, Compilation compilation, A

if (rootClass != null)
XmlnsHelper.ParseXmlns(rootClass.Value, out rootType, out rootClrNamespace, out var rootAsm, out var targetPlatform);
else if (hasXamlCompilationProcessingInstruction)
else if (hasXamlCompilationProcessingInstruction && root.NamespaceURI == XamlParser.MauiUri)
{
rootClrNamespace = "__XamlGeneratedCode__";
rootType = $"__Type{uid}";
Expand Down Expand Up @@ -325,13 +325,13 @@ static bool GetXamlCompilationProcessingInstruction(XmlDocument xmlDoc)
{
var instruction = xmlDoc.SelectSingleNode("processing-instruction('xaml-comp')") as XmlProcessingInstruction;
if (instruction == null)
return false;
return true;

var parts = instruction.Data.Split(' ', '=');
var indexOfCompile = Array.IndexOf(parts, "compile");
if (indexOfCompile != -1)
return parts[indexOfCompile + 1].Trim('"', '\'').Equals("true", StringComparison.OrdinalIgnoreCase);
return false;
return !parts[indexOfCompile + 1].Trim('"', '\'').Equals("false", StringComparison.OrdinalIgnoreCase);
return true;
}

static IEnumerable<(string name, string type, string accessModifier)> GetNamedFields(XmlNode root, XmlNamespaceManager nsmgr, Compilation compilation, AssemblyCaches caches, CancellationToken cancellationToken)
Expand Down
11 changes: 6 additions & 5 deletions src/Controls/tests/Xaml.UnitTests/CecilExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,27 +72,28 @@ EmbeddedResource GetResource(string name)
"IsCompiledDefault",
"X2006Namespace",
"X2009Primitives",
"Validation.MissingXClass",
};

[Test, TestCaseSource(nameof(IsXamlTrueSource))]
public void IsXamlTrue(string name)
{
var resource = GetResource(name);
Assert.IsTrue(resource.IsXaml(new XamlCache(), assembly.MainModule, out string className), $"IsXaml should return true for '{name}'.");
Assert.AreEqual(className, $"{testNamespace}.{name}"); // Test cases x:Class matches the file name
if (!className.StartsWith("__XamlGeneratedCode__"))
Assert.AreEqual(className, $"{testNamespace}.{name}"); // Test cases x:Class matches the file name
}

static string[] IsXamlFalseSource = new[]
{
"Validation.MissingXClass",
"Validation.NotXaml",
};

};
[Test, TestCaseSource(nameof(IsXamlFalseSource))]
public void IsXamlFalse(string name)
{
var resource = GetResource(name);
Assert.IsFalse(resource.IsXaml(new XamlCache(), assembly.MainModule, out _), $"IsXaml should return false for '{name}'.");
}
}
}
}
2 changes: 2 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/Issues/Gh13209.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public Gh13209(bool useCompiledXaml)
[TestFixture]
class Tests
{
[TearDown] public void TearDown() => ResourceDictionary.ClearCache();

[TestCase(true), TestCase(false)]
public void RdWithSource(bool useCompiledXaml)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Maui.Controls.Core.UnitTests;
using Microsoft.Maui.Graphics;
using NUnit.Framework;
using NUnit.Framework.Constraints;

namespace Microsoft.Maui.Controls.Xaml.UnitTests
{
Expand Down Expand Up @@ -48,7 +49,7 @@ public void XRIDIsGeneratedForRDWithoutCodeBehind()
var resourceId = XamlResourceIdAttribute.GetResourceIdForPath(asm, "AppResources/Colors.xaml");
Assert.That(resourceId, Is.Not.Null);
var type = XamlResourceIdAttribute.GetTypeForResourceId(asm, resourceId);
Assert.That(type, Is.Null);
Assert.That(type.Name, Does.StartWith("__Type")); //xaml-comp defult to true, so this is an autogenerated code behind
}

[Test]
Expand Down

0 comments on commit 6cb9048

Please sign in to comment.