diff --git a/src/Controls/src/SourceGen/CodeBehindGenerator.cs b/src/Controls/src/SourceGen/CodeBehindGenerator.cs index d45e2b271e52..83ed4b4e54e0 100644 --- a/src/Controls/src/SourceGen/CodeBehindGenerator.cs +++ b/src/Controls/src/SourceGen/CodeBehindGenerator.cs @@ -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}"; @@ -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) diff --git a/src/Controls/tests/Xaml.UnitTests/CecilExtensionsTests.cs b/src/Controls/tests/Xaml.UnitTests/CecilExtensionsTests.cs index eaa080ebb284..46db87bcff4a 100644 --- a/src/Controls/tests/Xaml.UnitTests/CecilExtensionsTests.cs +++ b/src/Controls/tests/Xaml.UnitTests/CecilExtensionsTests.cs @@ -72,6 +72,7 @@ EmbeddedResource GetResource(string name) "IsCompiledDefault", "X2006Namespace", "X2009Primitives", + "Validation.MissingXClass", }; [Test, TestCaseSource(nameof(IsXamlTrueSource))] @@ -79,20 +80,20 @@ 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}'."); - } + } } } diff --git a/src/Controls/tests/Xaml.UnitTests/Issues/Gh13209.xaml.cs b/src/Controls/tests/Xaml.UnitTests/Issues/Gh13209.xaml.cs index a0561bc49f2e..85c441028d2f 100644 --- a/src/Controls/tests/Xaml.UnitTests/Issues/Gh13209.xaml.cs +++ b/src/Controls/tests/Xaml.UnitTests/Issues/Gh13209.xaml.cs @@ -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) { diff --git a/src/Controls/tests/Xaml.UnitTests/ResourceDictionaryWithSource.xaml.cs b/src/Controls/tests/Xaml.UnitTests/ResourceDictionaryWithSource.xaml.cs index eb64a0153e0d..a1ffa4eb1071 100644 --- a/src/Controls/tests/Xaml.UnitTests/ResourceDictionaryWithSource.xaml.cs +++ b/src/Controls/tests/Xaml.UnitTests/ResourceDictionaryWithSource.xaml.cs @@ -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 { @@ -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]