diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Given_Grid.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Given_Grid.cs index ba4bbe0c30f2..ca7e1f316d31 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Given_Grid.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Given_Grid.cs @@ -15,10 +15,10 @@ public async Task When_Grid_Uses_Both_Syntaxes() { ExpectedDiagnostics = { - // Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator\Grid_Uses_Both_Syntaxes_794c1760299b374d12fe38ba3b633206.cs(105,5): error CS1912: Duplicate initialization of member 'ColumnDefinitions' - DiagnosticResult.CompilerError("CS1912").WithSpan(Path.Combine("Uno.UI.SourceGenerators","Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator","Grid_Uses_Both_Syntaxes_794c1760299b374d12fe38ba3b633206.cs"), 113, 5, 113, 22).WithArguments("ColumnDefinitions"), - // Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator\Grid_Uses_Both_Syntaxes_794c1760299b374d12fe38ba3b633206.cs(139,5): error CS1912: Duplicate initialization of member 'RowDefinitions' - DiagnosticResult.CompilerError("CS1912").WithSpan(Path.Combine("Uno.UI.SourceGenerators","Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator","Grid_Uses_Both_Syntaxes_794c1760299b374d12fe38ba3b633206.cs"), 147, 5, 147, 19).WithArguments("RowDefinitions"), + // Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator\Grid_Uses_Both_Syntaxes_794c1760299b374d12fe38ba3b633206.cs(120,5): error CS1912: Duplicate initialization of member 'ColumnDefinitions' + DiagnosticResult.CompilerError("CS1912").WithSpan(Path.Combine("Uno.UI.SourceGenerators","Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator","Grid_Uses_Both_Syntaxes_794c1760299b374d12fe38ba3b633206.cs"), 120, 5, 120, 22).WithArguments("ColumnDefinitions"), + // Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator\Grid_Uses_Both_Syntaxes_794c1760299b374d12fe38ba3b633206.cs(154,5): error CS1912: Duplicate initialization of member 'RowDefinitions' + DiagnosticResult.CompilerError("CS1912").WithSpan(Path.Combine("Uno.UI.SourceGenerators","Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator","Grid_Uses_Both_Syntaxes_794c1760299b374d12fe38ba3b633206.cs"), 154, 5, 154, 19).WithArguments("RowDefinitions"), }, }; diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Given_MvvmGeneratedMembers.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Given_MvvmGeneratedMembers.cs index cef10559aa99..9fbf45a7e4bd 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Given_MvvmGeneratedMembers.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Given_MvvmGeneratedMembers.cs @@ -154,12 +154,12 @@ public void MyBindBack(string s) { } }.AddGeneratedSources(); test.ExpectedDiagnostics.AddRange(new[] { - // Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator\MainPage_d6cd66944958ced0c513e0a04797b51d.cs(75,239): error CS1061: 'MyViewModel' does not contain a definition for 'Name' and no accessible extension method 'Name' accepting a first argument of type 'MyViewModel' could be found (are you missing a using directive or an assembly reference?) - DiagnosticResult.CompilerError("CS1061").WithSpan(@"Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator\MainPage_d6cd66944958ced0c513e0a04797b51d.cs", 72, 239, 72, 243).WithArguments("TestRepro.MyViewModel", "Name"), - // Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator\MainPage_d6cd66944958ced0c513e0a04797b51d.cs(97,239): error CS1061: 'MyViewModel' does not contain a definition for 'Name' and no accessible extension method 'Name' accepting a first argument of type 'MyViewModel' could be found (are you missing a using directive or an assembly reference?) - DiagnosticResult.CompilerError("CS1061").WithSpan(@"Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator\MainPage_d6cd66944958ced0c513e0a04797b51d.cs", 94, 239, 94, 243).WithArguments("TestRepro.MyViewModel", "Name"), - // Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator\MainPage_d6cd66944958ced0c513e0a04797b51d.cs(119,239): error CS1061: 'MyViewModel' does not contain a definition for 'Name' and no accessible extension method 'Name' accepting a first argument of type 'MyViewModel' could be found (are you missing a using directive or an assembly reference?) - DiagnosticResult.CompilerError("CS1061").WithSpan(@"Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator\MainPage_d6cd66944958ced0c513e0a04797b51d.cs", 116, 239, 116, 243).WithArguments("TestRepro.MyViewModel", "Name"), + // Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator\MainPage_d6cd66944958ced0c513e0a04797b51d.cs(79,239): error CS1061: 'MyViewModel' does not contain a definition for 'Name' and no accessible extension method 'Name' accepting a first argument of type 'MyViewModel' could be found (are you missing a using directive or an assembly reference?) + DiagnosticResult.CompilerError("CS1061").WithSpan(@"Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator\MainPage_d6cd66944958ced0c513e0a04797b51d.cs", 79, 239, 79, 243).WithArguments("TestRepro.MyViewModel", "Name"), + // Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator\MainPage_d6cd66944958ced0c513e0a04797b51d.cs(101,239): error CS1061: 'MyViewModel' does not contain a definition for 'Name' and no accessible extension method 'Name' accepting a first argument of type 'MyViewModel' could be found (are you missing a using directive or an assembly reference?) + DiagnosticResult.CompilerError("CS1061").WithSpan(@"Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator\MainPage_d6cd66944958ced0c513e0a04797b51d.cs", 101, 239, 101, 243).WithArguments("TestRepro.MyViewModel", "Name"), + // Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator\MainPage_d6cd66944958ced0c513e0a04797b51d.cs(123,239): error CS1061: 'MyViewModel' does not contain a definition for 'Name' and no accessible extension method 'Name' accepting a first argument of type 'MyViewModel' could be found (are you missing a using directive or an assembly reference?) + DiagnosticResult.CompilerError("CS1061").WithSpan(@"Uno.UI.SourceGenerators\Uno.UI.SourceGenerators.XamlGenerator.XamlCodeGenerator\MainPage_d6cd66944958ced0c513e0a04797b51d.cs", 123, 239, 123, 243).WithArguments("TestRepro.MyViewModel", "Name"), }); await test.RunAsync(); diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/SBUIIOFFE/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/SBUIIOFFE/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs index bfc9addda4b5..fdfd8e629246 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/SBUIIOFFE/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/SBUIIOFFE/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs @@ -43,7 +43,7 @@ partial class MainPage : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { var __resourceLocator = new global::System.Uri("file:///C:/Project/0/MainPage.xaml"); @@ -52,7 +52,14 @@ private void InitializeComponent() global::Microsoft.UI.Xaml.Application.LoadComponent(this, __resourceLocator); return; } - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; // Source 0\MainPage.xaml (Line 1:2) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/SOSLIIOFDOTAFE/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/SOSLIIOFDOTAFE/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs index 2cf43153c9d9..d8fe8b632723 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/SOSLIIOFDOTAFE/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/SOSLIIOFDOTAFE/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs @@ -43,7 +43,7 @@ partial class MainPage : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { var __resourceLocator = new global::System.Uri("file:///C:/Project/0/MainPage.xaml"); @@ -52,7 +52,14 @@ private void InitializeComponent() global::Microsoft.UI.Xaml.Application.LoadComponent(this, __resourceLocator); return; } - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; Resources[ @@ -322,7 +329,7 @@ private class _MainPage_d6cd66944958ced0c513e0a04797b51d_TestReproMainPageSC0 : private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); public _View Build(object __ResourceOwner_1) { _View __rootInstance = null; @@ -392,7 +399,7 @@ public _View Build(object __ResourceOwner_1) if (global::Microsoft.UI.Xaml.NameScope.GetNameScope(d) == null) { global::Microsoft.UI.Xaml.NameScope.SetNameScope(d, __nameScope); - __nameScope.Owner = d; + ((global::Microsoft.UI.Xaml.NameScope)__nameScope).Owner = d; } global::Uno.UI.FrameworkElementHelper.AddObjectReference(d, this); } @@ -423,7 +430,7 @@ private class _MainPage_d6cd66944958ced0c513e0a04797b51d_TestReproMainPageSC1 : private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); public _View Build(object __ResourceOwner_1) { _View __rootInstance = null; @@ -447,7 +454,7 @@ public _View Build(object __ResourceOwner_1) if (global::Microsoft.UI.Xaml.NameScope.GetNameScope(d) == null) { global::Microsoft.UI.Xaml.NameScope.SetNameScope(d, __nameScope); - __nameScope.Owner = d; + ((global::Microsoft.UI.Xaml.NameScope)__nameScope).Owner = d; } global::Uno.UI.FrameworkElementHelper.AddObjectReference(d, this); } diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/SOSLIOFPLR/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/SOSLIOFPLR/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs index 439cad475909..6842a828e71d 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/SOSLIOFPLR/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/SOSLIOFPLR/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs @@ -43,7 +43,7 @@ partial class MainPage : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { var __resourceLocator = new global::System.Uri("file:///C:/Project/0/MainPage.xaml"); @@ -52,7 +52,14 @@ private void InitializeComponent() global::Microsoft.UI.Xaml.Application.LoadComponent(this, __resourceLocator); return; } - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; Resources[ @@ -177,7 +184,7 @@ private class _MainPage_d6cd66944958ced0c513e0a04797b51d_TestReproMainPageSC0 : private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); public _View Build(object __ResourceOwner_1) { _View __rootInstance = null; @@ -247,7 +254,7 @@ public _View Build(object __ResourceOwner_1) if (global::Microsoft.UI.Xaml.NameScope.GetNameScope(d) == null) { global::Microsoft.UI.Xaml.NameScope.SetNameScope(d, __nameScope); - __nameScope.Owner = d; + ((global::Microsoft.UI.Xaml.NameScope)__nameScope).Owner = d; } global::Uno.UI.FrameworkElementHelper.AddObjectReference(d, this); } diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/SOSLIOFPLS/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/SOSLIOFPLS/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs index 694c66d4798c..15e726302085 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/SOSLIOFPLS/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/SOSLIOFPLS/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs @@ -43,7 +43,7 @@ partial class MainPage : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { var __resourceLocator = new global::System.Uri("file:///C:/Project/0/MainPage.xaml"); @@ -52,7 +52,14 @@ private void InitializeComponent() global::Microsoft.UI.Xaml.Application.LoadComponent(this, __resourceLocator); return; } - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; Resources[ diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/SOSLIOFRT/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/SOSLIOFRT/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs index 50a55691e98c..30d0ecad2cc8 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/SOSLIOFRT/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/SOSLIOFRT/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs @@ -43,7 +43,7 @@ partial class MainPage : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { var __resourceLocator = new global::System.Uri("file:///C:/Project/0/MainPage.xaml"); @@ -52,7 +52,14 @@ private void InitializeComponent() global::Microsoft.UI.Xaml.Application.LoadComponent(this, __resourceLocator); return; } - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; Resources[ diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/SOSLIOFTLRD/XamlCodeGenerator_MyDictionary_b7707bcf1e73425b710b6a5d04177088.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/SOSLIOFTLRD/XamlCodeGenerator_MyDictionary_b7707bcf1e73425b710b6a5d04177088.cs index bbd740570a90..a6942a4f29aa 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/SOSLIOFTLRD/XamlCodeGenerator_MyDictionary_b7707bcf1e73425b710b6a5d04177088.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/SOSLIOFTLRD/XamlCodeGenerator_MyDictionary_b7707bcf1e73425b710b6a5d04177088.cs @@ -62,7 +62,7 @@ public sealed partial class GlobalStaticResources [global::System.Runtime.CompilerServices.CreateNewOnMetadataUpdate] internal sealed class ResourceDictionarySingleton__MyDictionary_b7707bcf1e73425b710b6a5d04177088 : global::Uno.UI.IXamlResourceDictionaryProvider { - private static global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private static global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private static global::Uno.UI.IXamlResourceDictionaryProvider __that; internal static global::Uno.UI.IXamlResourceDictionaryProvider Instance { diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TAA/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TAA/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs index 10acc6e5c487..72399bfa585a 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TAA/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TAA/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs @@ -40,10 +40,17 @@ partial class MainPage : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; // Source 0\MainPage.xaml (Line 1:2) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TBTNSICB/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TBTNSICB/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs index 6c14998314fa..0501d88e6d5d 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TBTNSICB/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TBTNSICB/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs @@ -40,10 +40,17 @@ partial class MainPage : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; // Source 0\MainPage.xaml (Line 1:2) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TBTNSICB/XamlCodeGenerator_UserControl1_81d855d5b3bba02f594dcda3b149beb2.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TBTNSICB/XamlCodeGenerator_UserControl1_81d855d5b3bba02f594dcda3b149beb2.cs index 8bbfc030fecc..f20eb4ae77b7 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TBTNSICB/XamlCodeGenerator_UserControl1_81d855d5b3bba02f594dcda3b149beb2.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TBTNSICB/XamlCodeGenerator_UserControl1_81d855d5b3bba02f594dcda3b149beb2.cs @@ -40,10 +40,17 @@ partial class UserControl1 : global::Microsoft.UI.Xaml.Controls.UserControl private const string __baseUri_prefix_UserControl1_81d855d5b3bba02f594dcda3b149beb2 = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_UserControl1_81d855d5b3bba02f594dcda3b149beb2 = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; // Source 0\UserControl1.xaml (Line 1:2) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TDBMIDTIRD/XamlCodeGenerator_MyResourceDictionary_92716e07ff456818f6d4125e055d4d57.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TDBMIDTIRD/XamlCodeGenerator_MyResourceDictionary_92716e07ff456818f6d4125e055d4d57.cs index 807ba550ca6a..163358ce0bda 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TDBMIDTIRD/XamlCodeGenerator_MyResourceDictionary_92716e07ff456818f6d4125e055d4d57.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TDBMIDTIRD/XamlCodeGenerator_MyResourceDictionary_92716e07ff456818f6d4125e055d4d57.cs @@ -58,7 +58,7 @@ private class _MyResourceDictionary_92716e07ff456818f6d4125e055d4d57_TestReproMy private const string __baseUri_prefix_MyResourceDictionary_92716e07ff456818f6d4125e055d4d57 = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MyResourceDictionary_92716e07ff456818f6d4125e055d4d57 = "ms-appx:///TestProject/"; - global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); public _View Build(object __ResourceOwner_1) { _View __rootInstance = null; @@ -103,7 +103,7 @@ public _View Build(object __ResourceOwner_1) if (global::Microsoft.UI.Xaml.NameScope.GetNameScope(d) == null) { global::Microsoft.UI.Xaml.NameScope.SetNameScope(d, __nameScope); - __nameScope.Owner = d; + ((global::Microsoft.UI.Xaml.NameScope)__nameScope).Owner = d; } global::Uno.UI.FrameworkElementHelper.AddObjectReference(d, this); } @@ -153,7 +153,7 @@ public sealed partial class GlobalStaticResources // This non-static inner class is a means of reducing size of AOT compilations by avoiding many accesses to static members from a static callsite, which adds costly class initializer checks each time. internal sealed class ResourceDictionarySingleton__MyResourceDictionary_92716e07ff456818f6d4125e055d4d57 : global::Uno.UI.IXamlResourceDictionaryProvider { - private static global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private static global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private static global::Uno.UI.IXamlResourceDictionaryProvider __that; internal static global::Uno.UI.IXamlResourceDictionaryProvider Instance { @@ -224,7 +224,7 @@ class _MyResourceDictionary_92716e07ff456818f6d4125e055d4d57_MyResourceDictionar private const string __baseUri_prefix_MyResourceDictionary_92716e07ff456818f6d4125e055d4d57 = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MyResourceDictionary_92716e07ff456818f6d4125e055d4d57 = "ms-appx:///TestProject/"; - global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); public _View Build(object __ResourceOwner_1) { _View __rootInstance = null; @@ -269,7 +269,7 @@ public _View Build(object __ResourceOwner_1) if (global::Microsoft.UI.Xaml.NameScope.GetNameScope(d) == null) { global::Microsoft.UI.Xaml.NameScope.SetNameScope(d, __nameScope); - __nameScope.Owner = d; + ((global::Microsoft.UI.Xaml.NameScope)__nameScope).Owner = d; } global::Uno.UI.FrameworkElementHelper.AddObjectReference(d, this); } @@ -313,7 +313,7 @@ class _MyResourceDictionary_92716e07ff456818f6d4125e055d4d57_MyResourceDictionar private const string __baseUri_prefix_MyResourceDictionary_92716e07ff456818f6d4125e055d4d57 = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MyResourceDictionary_92716e07ff456818f6d4125e055d4d57 = "ms-appx:///TestProject/"; - global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); public _View Build(object __ResourceOwner_1) { _View __rootInstance = null; @@ -358,7 +358,7 @@ public _View Build(object __ResourceOwner_1) if (global::Microsoft.UI.Xaml.NameScope.GetNameScope(d) == null) { global::Microsoft.UI.Xaml.NameScope.SetNameScope(d, __nameScope); - __nameScope.Owner = d; + ((global::Microsoft.UI.Xaml.NameScope)__nameScope).Owner = d; } global::Uno.UI.FrameworkElementHelper.AddObjectReference(d, this); } diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TIM/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TIM/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs index 208a229f0682..056e6a2d3af2 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TIM/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TIM/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs @@ -40,10 +40,17 @@ partial class MainPage : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; // Source 0\MainPage.xaml (Line 1:2) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TNMEIAP/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TNMEIAP/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs index a00781b14041..bf598be8554c 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TNMEIAP/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TNMEIAP/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs @@ -40,10 +40,17 @@ partial class MainPage : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; // Source 0\MainPage.xaml (Line 1:2) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TNMEIRP/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TNMEIRP/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs index 507c3e1ad5fd..84cb7b101280 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TNMEIRP/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TNMEIRP/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs @@ -40,10 +40,17 @@ partial class MainPage : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; // Source 0\MainPage.xaml (Line 1:2) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TPXBRXLE/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TPXBRXLE/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs index 009213b29604..057c8f7adc86 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TPXBRXLE/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TPXBRXLE/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs @@ -40,10 +40,17 @@ partial class MainPage : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; Resources[ diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TRDAAPSSTP/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TRDAAPSSTP/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs index d5f028ac8f43..06752b628104 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TRDAAPSSTP/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TRDAAPSSTP/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs @@ -40,10 +40,17 @@ partial class MainPage : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; // Source 0\MainPage.xaml (Line 1:2) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TTLNMD/XamlCodeGenerator_MyResourceDictionary_92716e07ff456818f6d4125e055d4d57.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TTLNMD/XamlCodeGenerator_MyResourceDictionary_92716e07ff456818f6d4125e055d4d57.cs index 93c9b7a4e3ba..e72913986546 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TTLNMD/XamlCodeGenerator_MyResourceDictionary_92716e07ff456818f6d4125e055d4d57.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TTLNMD/XamlCodeGenerator_MyResourceDictionary_92716e07ff456818f6d4125e055d4d57.cs @@ -43,7 +43,7 @@ public sealed partial class GlobalStaticResources // This non-static inner class is a means of reducing size of AOT compilations by avoiding many accesses to static members from a static callsite, which adds costly class initializer checks each time. internal sealed class ResourceDictionarySingleton__MyResourceDictionary_92716e07ff456818f6d4125e055d4d57 : global::Uno.UI.IXamlResourceDictionaryProvider { - private static global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private static global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private static global::Uno.UI.IXamlResourceDictionaryProvider __that; internal static global::Uno.UI.IXamlResourceDictionaryProvider Instance { diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TTLNMDWSRD/XamlCodeGenerator_MyResourceDictionary_92716e07ff456818f6d4125e055d4d57.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TTLNMDWSRD/XamlCodeGenerator_MyResourceDictionary_92716e07ff456818f6d4125e055d4d57.cs index 588731e0a0bd..90231348a47a 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TTLNMDWSRD/XamlCodeGenerator_MyResourceDictionary_92716e07ff456818f6d4125e055d4d57.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TTLNMDWSRD/XamlCodeGenerator_MyResourceDictionary_92716e07ff456818f6d4125e055d4d57.cs @@ -43,7 +43,7 @@ public sealed partial class GlobalStaticResources // This non-static inner class is a means of reducing size of AOT compilations by avoiding many accesses to static members from a static callsite, which adds costly class initializer checks each time. internal sealed class ResourceDictionarySingleton__MyResourceDictionary_92716e07ff456818f6d4125e055d4d57 : global::Uno.UI.IXamlResourceDictionaryProvider { - private static global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private static global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private static global::Uno.UI.IXamlResourceDictionaryProvider __that; internal static global::Uno.UI.IXamlResourceDictionaryProvider Instance { diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TTLNMDWTRD/XamlCodeGenerator_MyResourceDictionary_92716e07ff456818f6d4125e055d4d57.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TTLNMDWTRD/XamlCodeGenerator_MyResourceDictionary_92716e07ff456818f6d4125e055d4d57.cs index 8ba15e0dbab7..fdb7227d3acc 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TTLNMDWTRD/XamlCodeGenerator_MyResourceDictionary_92716e07ff456818f6d4125e055d4d57.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TTLNMDWTRD/XamlCodeGenerator_MyResourceDictionary_92716e07ff456818f6d4125e055d4d57.cs @@ -43,7 +43,7 @@ public sealed partial class GlobalStaticResources // This non-static inner class is a means of reducing size of AOT compilations by avoiding many accesses to static members from a static callsite, which adds costly class initializer checks each time. internal sealed class ResourceDictionarySingleton__MyResourceDictionary_92716e07ff456818f6d4125e055d4d57 : global::Uno.UI.IXamlResourceDictionaryProvider { - private static global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private static global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private static global::Uno.UI.IXamlResourceDictionaryProvider __that; internal static global::Uno.UI.IXamlResourceDictionaryProvider Instance { diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TTPWXS/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TTPWXS/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs index e68fe6e80e21..1869b5298e67 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TTPWXS/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TTPWXS/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs @@ -40,10 +40,17 @@ partial class MainPage : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; // Source 0\MainPage.xaml (Line 1:2) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TTW/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TTW/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs index 58382da55e61..ac53412f834f 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TTW/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TTW/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs @@ -40,10 +40,17 @@ partial class MainPage : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; // Source 0\MainPage.xaml (Line 1:2) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TVBP/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TVBP/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs index ea6db7c4cd4f..d7429a80a417 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TVBP/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TVBP/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs @@ -40,10 +40,17 @@ partial class MainPage : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; // Source 0\MainPage.xaml (Line 1:2) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TXBRXLE/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TXBRXLE/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs index 578464cc5d6b..86b9a4bf73ee 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TXBRXLE/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/TXBRXLE/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs @@ -40,10 +40,17 @@ partial class MainPage : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; Resources[ diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WADPIT/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WADPIT/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs index ae8d957600b2..c1d65cce4a16 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WADPIT/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WADPIT/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs @@ -40,10 +40,17 @@ partial class MainPage : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; // Source 0\MainPage.xaml (Line 1:2) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WBENIT/XamlCodeGenerator_Binding_ElementName_In_Template_66bf0a54f1801c397a6fa4930a237eca.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WBENIT/XamlCodeGenerator_Binding_ElementName_In_Template_66bf0a54f1801c397a6fa4930a237eca.cs index 2bfcbd8de737..047b93fc449a 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WBENIT/XamlCodeGenerator_Binding_ElementName_In_Template_66bf0a54f1801c397a6fa4930a237eca.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WBENIT/XamlCodeGenerator_Binding_ElementName_In_Template_66bf0a54f1801c397a6fa4930a237eca.cs @@ -40,10 +40,17 @@ partial class Binding_ElementName_In_Template : global::Microsoft.UI.Xaml.Contro private const string __baseUri_prefix_Binding_ElementName_In_Template_66bf0a54f1801c397a6fa4930a237eca = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_Binding_ElementName_In_Template_66bf0a54f1801c397a6fa4930a237eca = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; // Source 0\Binding_ElementName_In_Template.xaml (Line 1:2) @@ -122,7 +129,7 @@ private class _Binding_ElementName_In_Template_66bf0a54f1801c397a6fa4930a237eca_ private const string __baseUri_prefix_Binding_ElementName_In_Template_66bf0a54f1801c397a6fa4930a237eca = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_Binding_ElementName_In_Template_66bf0a54f1801c397a6fa4930a237eca = "ms-appx:///TestProject/"; - global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); public _View Build(object __ResourceOwner_1) { _View __rootInstance = null; @@ -167,7 +174,7 @@ public _View Build(object __ResourceOwner_1) if (global::Microsoft.UI.Xaml.NameScope.GetNameScope(d) == null) { global::Microsoft.UI.Xaml.NameScope.SetNameScope(d, __nameScope); - __nameScope.Owner = d; + ((global::Microsoft.UI.Xaml.NameScope)__nameScope).Owner = d; } global::Uno.UI.FrameworkElementHelper.AddObjectReference(d, this); } diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WGUBS/XamlCodeGenerator_Grid_Uses_Both_Syntaxes_794c1760299b374d12fe38ba3b633206.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WGUBS/XamlCodeGenerator_Grid_Uses_Both_Syntaxes_794c1760299b374d12fe38ba3b633206.cs index dbe76c567640..c223b0764953 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WGUBS/XamlCodeGenerator_Grid_Uses_Both_Syntaxes_794c1760299b374d12fe38ba3b633206.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WGUBS/XamlCodeGenerator_Grid_Uses_Both_Syntaxes_794c1760299b374d12fe38ba3b633206.cs @@ -40,10 +40,17 @@ partial class Grid_Uses_Both_Syntaxes : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_Grid_Uses_Both_Syntaxes_794c1760299b374d12fe38ba3b633206 = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_Grid_Uses_Both_Syntaxes_794c1760299b374d12fe38ba3b633206 = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; // Source 0\Grid_Uses_Both_Syntaxes.xaml (Line 1:2) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WGUCS/XamlCodeGenerator_Grid_Uses_Common_Syntax_8659265dc2cabf0b5a684f0f4b26f8b7.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WGUCS/XamlCodeGenerator_Grid_Uses_Common_Syntax_8659265dc2cabf0b5a684f0f4b26f8b7.cs index 401376f031eb..33d7ffa52786 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WGUCS/XamlCodeGenerator_Grid_Uses_Common_Syntax_8659265dc2cabf0b5a684f0f4b26f8b7.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WGUCS/XamlCodeGenerator_Grid_Uses_Common_Syntax_8659265dc2cabf0b5a684f0f4b26f8b7.cs @@ -40,10 +40,17 @@ partial class Grid_Uses_Common_Syntax : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_Grid_Uses_Common_Syntax_8659265dc2cabf0b5a684f0f4b26f8b7 = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_Grid_Uses_Common_Syntax_8659265dc2cabf0b5a684f0f4b26f8b7 = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; // Source 0\Grid_Uses_Common_Syntax.xaml (Line 1:2) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WGUNACPS/XamlCodeGenerator_Grid_Uses_New_Assigned_ContentProperty_Syntax_9f853e56098ff21d39f15479035509ef.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WGUNACPS/XamlCodeGenerator_Grid_Uses_New_Assigned_ContentProperty_Syntax_9f853e56098ff21d39f15479035509ef.cs index 754bf1215ac5..de15e3b94cea 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WGUNACPS/XamlCodeGenerator_Grid_Uses_New_Assigned_ContentProperty_Syntax_9f853e56098ff21d39f15479035509ef.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WGUNACPS/XamlCodeGenerator_Grid_Uses_New_Assigned_ContentProperty_Syntax_9f853e56098ff21d39f15479035509ef.cs @@ -40,10 +40,17 @@ partial class Grid_Uses_New_Assigned_ContentProperty_Syntax : global::Microsoft. private const string __baseUri_prefix_Grid_Uses_New_Assigned_ContentProperty_Syntax_9f853e56098ff21d39f15479035509ef = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_Grid_Uses_New_Assigned_ContentProperty_Syntax_9f853e56098ff21d39f15479035509ef = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; // Source 0\Grid_Uses_New_Assigned_ContentProperty_Syntax.xaml (Line 1:2) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WGUNSS/XamlCodeGenerator_Grid_Uses_New_Succinct_Syntax_cc38332c9fd4c0da9c1fa0270bbd3609.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WGUNSS/XamlCodeGenerator_Grid_Uses_New_Succinct_Syntax_cc38332c9fd4c0da9c1fa0270bbd3609.cs index 3838abc3ae60..d10eb5fd6e7b 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WGUNSS/XamlCodeGenerator_Grid_Uses_New_Succinct_Syntax_cc38332c9fd4c0da9c1fa0270bbd3609.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WGUNSS/XamlCodeGenerator_Grid_Uses_New_Succinct_Syntax_cc38332c9fd4c0da9c1fa0270bbd3609.cs @@ -40,10 +40,17 @@ partial class Grid_Uses_New_Succinct_Syntax : global::Microsoft.UI.Xaml.Controls private const string __baseUri_prefix_Grid_Uses_New_Succinct_Syntax_cc38332c9fd4c0da9c1fa0270bbd3609 = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_Grid_Uses_New_Succinct_Syntax_cc38332c9fd4c0da9c1fa0270bbd3609 = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; // Source 0\Grid_Uses_New_Succinct_Syntax.xaml (Line 1:2) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WI/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WI/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs index 1e27d3994324..71c12ad0650c 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WI/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WI/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs @@ -40,10 +40,17 @@ partial class MainPage : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; // Source 0\MainPage.xaml (Line 1:2) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WNIONE/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WNIONE/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs index 5123a84c3951..ba9fc56ef122 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WNIONE/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WNIONE/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs @@ -40,10 +40,17 @@ partial class MainPage : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; // Source 0\MainPage.xaml (Line 1:2) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WNS/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WNS/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs index c3a0c529f28f..60ba8e1de3d4 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WNS/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WNS/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs @@ -40,10 +40,17 @@ partial class MainPage : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; Resources[ diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WOPADNE/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WOPADNE/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs index 7a7a8db717d9..0b91caf7191c 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WOPADNE/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WOPADNE/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs @@ -40,10 +40,17 @@ partial class MainPage : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; // Source 0\MainPage.xaml (Line 1:2) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WOPAE__name/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WOPAE__name/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs index 7a7a8db717d9..0b91caf7191c 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WOPAE__name/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WOPAE__name/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs @@ -40,10 +40,17 @@ partial class MainPage : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; // Source 0\MainPage.xaml (Line 1:2) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WOPAE_m_name/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WOPAE_m_name/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs index 7a7a8db717d9..0b91caf7191c 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WOPAE_m_name/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WOPAE_m_name/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs @@ -40,10 +40,17 @@ partial class MainPage : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; // Source 0\MainPage.xaml (Line 1:2) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WOPAE_name/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WOPAE_name/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs index 7a7a8db717d9..0b91caf7191c 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WOPAE_name/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WOPAE_name/XamlCodeGenerator_MainPage_d6cd66944958ced0c513e0a04797b51d.cs @@ -40,10 +40,17 @@ partial class MainPage : global::Microsoft.UI.Xaml.Controls.Page private const string __baseUri_prefix_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_MainPage_d6cd66944958ced0c513e0a04797b51d = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; // Source 0\MainPage.xaml (Line 1:2) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WXOWCP/XamlCodeGenerator_Binding_Xaml_Object_With_Common_Properties_4891310bc693a433ba9a8e9f5113f94f.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WXOWCP/XamlCodeGenerator_Binding_Xaml_Object_With_Common_Properties_4891310bc693a433ba9a8e9f5113f94f.cs index a556e60b44d6..c21249af9b41 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WXOWCP/XamlCodeGenerator_Binding_Xaml_Object_With_Common_Properties_4891310bc693a433ba9a8e9f5113f94f.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WXOWCP/XamlCodeGenerator_Binding_Xaml_Object_With_Common_Properties_4891310bc693a433ba9a8e9f5113f94f.cs @@ -40,10 +40,17 @@ partial class Binding_Xaml_Object_With_Common_Properties : global::Microsoft.UI. private const string __baseUri_prefix_Binding_Xaml_Object_With_Common_Properties_4891310bc693a433ba9a8e9f5113f94f = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_Binding_Xaml_Object_With_Common_Properties_4891310bc693a433ba9a8e9f5113f94f = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; Resources[ diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WXOWXOP/XamlCodeGenerator_Binding_Xaml_Object_With_Xaml_Object_Properties_5147419e44d1bc3e3f86860ad528476f.cs b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WXOWXOP/XamlCodeGenerator_Binding_Xaml_Object_With_Xaml_Object_Properties_5147419e44d1bc3e3f86860ad528476f.cs index beefe344295b..4e1bed968190 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WXOWXOP/XamlCodeGenerator_Binding_Xaml_Object_With_Xaml_Object_Properties_5147419e44d1bc3e3f86860ad528476f.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators.Tests/XamlCodeGeneratorTests/Out/WXOWXOP/XamlCodeGenerator_Binding_Xaml_Object_With_Xaml_Object_Properties_5147419e44d1bc3e3f86860ad528476f.cs @@ -40,10 +40,17 @@ partial class Binding_Xaml_Object_With_Xaml_Object_Properties : global::Microsof private const string __baseUri_prefix_Binding_Xaml_Object_With_Xaml_Object_Properties_5147419e44d1bc3e3f86860ad528476f = "ms-appx:///TestProject/"; [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] private const string __baseUri_Binding_Xaml_Object_With_Xaml_Object_Properties_5147419e44d1bc3e3f86860ad528476f = "ms-appx:///TestProject/"; - private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); + private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope(); private void InitializeComponent() { - NameScope.SetNameScope(this, __nameScope); + if (NameScope.GetNameScope(this) is { } existingRootNameScope) + { + __nameScope = existingRootNameScope; + } + else + { + NameScope.SetNameScope(this, __nameScope); + } var __that = this; base.IsParsing = true; // Source 0\Binding_Xaml_Object_With_Xaml_Object_Properties.xaml (Line 1:2) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs b/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs index b122c41cee85..566ce0305c74 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs @@ -433,7 +433,7 @@ private void BuildBaseUri(IIndentedStringBuilder writer) private void BuildInitializeComponent(IndentedStringBuilder writer, XamlObjectDefinition topLevelControl, INamedTypeSymbol controlBaseType) { - writer.AppendLineIndented("private global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope();"); + writer.AppendLineIndented("private global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope();"); using (writer.BlockInvariant($"private void InitializeComponent()")) { @@ -753,7 +753,16 @@ private void BuildGenericControlInitializerBody(IndentedStringBuilder writer, Xa var topLevelControlType = GetType(topLevelControl.Type); if (!IsWindow(topLevelControlType)) // Window is not a DependencyObject { - writer.AppendLineIndented("NameScope.SetNameScope(this, __nameScope);"); + // In VisualTree.AddRoot, we may have already set the NameScope. + // In this case, use the existing namescope. + using (writer.BlockInvariant("if (NameScope.GetNameScope(this) is {{ }} existingRootNameScope)")) + { + writer.AppendLineIndented("__nameScope = existingRootNameScope;"); + } + using (writer.BlockInvariant("else")) + { + writer.AppendLineIndented("NameScope.SetNameScope(this, __nameScope);"); + } } writer.AppendLineIndented("var __that = this;"); TrySetParsing(writer, topLevelControlType, isInitializer: false); @@ -791,9 +800,19 @@ private void BuildGenericControlInitializerBody(IndentedStringBuilder writer, Xa if (IsWindow(topLevelControlType)) // Window is not a DependencyObject { - writer.AppendLineIndented("if (__that.Content != null)"); - using var _ = writer.Block(); - writer.AppendLineIndented("NameScope.SetNameScope(__that.Content, __nameScope);"); + using (writer.BlockInvariant("if (__that.Content != null)")) + { + // In VisualTree.AddRoot, we may have already set the NameScope. + // In this case, use the existing namescope. + using (writer.BlockInvariant("if (NameScope.GetNameScope(__that.Content) is {{ }} existingRootNameScope)")) + { + writer.AppendLineIndented("__nameScope = existingRootNameScope;"); + } + using (writer.BlockInvariant("else")) + { + writer.AppendLineIndented("NameScope.SetNameScope(__that.Content, __nameScope);"); + } + } } writer.AppendLineIndented("OnInitializeCompleted();"); @@ -914,7 +933,7 @@ private void BuildChildSubclasses(IIndentedStringBuilder writer, bool isTopLevel using (ResourceOwnerScope()) { - writer.AppendLineIndented("global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope();"); + writer.AppendLineIndented("global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope();"); using (writer.BlockInvariant($"public {kvp.Value.ReturnType} Build(object {CurrentResourceOwner})")) { @@ -934,7 +953,7 @@ private void BuildChildSubclasses(IIndentedStringBuilder writer, bool isTopLevel using (writer.BlockInvariant("if (global::Microsoft.UI.Xaml.NameScope.GetNameScope(d) == null)", kvp.Value.ReturnType)) { writer.AppendLineIndented("global::Microsoft.UI.Xaml.NameScope.SetNameScope(d, __nameScope);"); - writer.AppendLineIndented("__nameScope.Owner = d;"); + writer.AppendLineIndented("((global::Microsoft.UI.Xaml.NameScope)__nameScope).Owner = d;"); } writer.AppendLineIndented("global::Uno.UI.FrameworkElementHelper.AddObjectReference(d, this);"); @@ -1307,7 +1326,7 @@ IDisposable WrapSingleton() using (WrapSingleton()) { // Build singleton - writer.AppendLineInvariantIndented("private static global::Microsoft.UI.Xaml.NameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope();"); + writer.AppendLineInvariantIndented("private static global::Microsoft.UI.Xaml.Markup.INameScope __nameScope = new global::Microsoft.UI.Xaml.NameScope();"); writer.AppendLineInvariantIndented("private static {0} __that;", DictionaryProviderInterfaceName); using (writer.BlockInvariant("internal static {0} Instance", DictionaryProviderInterfaceName)) { diff --git a/src/Uno.UI.RuntimeTests/Tests/BindingTests/BindingTests.cs b/src/Uno.UI.RuntimeTests/Tests/BindingTests/BindingTests.cs index 77b1a3fc8ba0..db284ecdd1b9 100644 --- a/src/Uno.UI.RuntimeTests/Tests/BindingTests/BindingTests.cs +++ b/src/Uno.UI.RuntimeTests/Tests/BindingTests/BindingTests.cs @@ -6,7 +6,9 @@ using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Automation; using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Data; using Microsoft.UI.Xaml.Media; +using Private.Infrastructure; using SamplesApp.UITests; using Uno.UI.RuntimeTests.Helpers; @@ -16,6 +18,109 @@ namespace Uno.UI.RuntimeTests.Tests; [RunsOnUIThread] public class BindingTests { + [TestMethod] + public async Task When_Binding_By_Programmatically_Setting_Name() + { + var tb = new TextBox(); + tb.Name = "textBox"; + tb.Text = "Text"; + tb.DataContext = "Hello World"; + + var button = new Button(); + button.Name = "button"; + button.SetBinding(Button.ContentProperty, new Binding() + { + ElementName = "textBox", + Path = new PropertyPath("DataContext") + }); + + var sp = new StackPanel() + { + Children = + { + tb, + button, + }, + }; + + Assert.AreEqual(null, button.Content); + + await UITestHelper.Load(sp); + +#if UNO_HAS_ENHANCED_LIFECYCLE || WINAPPSDK + Assert.AreEqual("Hello World", button.Content); +#else + // Unfortunate wrong behavior on Android and iOS. + Assert.AreEqual(null, button.Content); +#endif + + tb.DataContext = "Hello World Updated"; + +#if UNO_HAS_ENHANCED_LIFECYCLE || WINAPPSDK + Assert.AreEqual("Hello World Updated", button.Content); +#else + // Unfortunate wrong behavior on Android and iOS. + Assert.AreEqual(null, button.Content); +#endif + + sp.Children.Remove(tb); + +#if UNO_HAS_ENHANCED_LIFECYCLE || WINAPPSDK + Assert.AreEqual("Hello World Updated", button.Content); +#else + // Unfortunate wrong behavior on Android and iOS. + Assert.AreEqual(null, button.Content); +#endif + + // At this point, the textBox name is was unregistered as it's removed from the visual tree. + // However, on WinUI, the binding have already solved the target for ElementName and the binding will continue to work. + tb.DataContext = "Hello World Updated 2"; + +#if UNO_HAS_ENHANCED_LIFECYCLE || WINAPPSDK + Assert.AreEqual("Hello World Updated 2", button.Content); +#else + // Unfortunate wrong behavior on Android and iOS. + Assert.AreEqual(null, button.Content); +#endif + } + + [TestMethod] + public async Task When_Binding_Is_Set_After_RegisterName_And_UnregisterName() + { + var tb = new TextBox(); + tb.Name = "textBox"; + tb.Text = "Text"; + tb.DataContext = "Hello World"; + + var button = new Button(); + button.Name = "button"; + + var sp = new StackPanel() + { + Children = + { + tb, + button, + }, + }; + + Assert.AreEqual(null, button.Content); + + await UITestHelper.Load(sp); + + sp.Children.Remove(tb); + + // The remove call will UnregisterName for the textBox. + // The binding shouldn't be able to find the target for ElementName + button.SetBinding(Button.ContentProperty, new Binding() + { + ElementName = "textBox", + Path = new PropertyPath("DataContext") + }); + + Assert.AreEqual(null, button.Content); + } + [TestMethod] public async Task When_Binding_Setter_Value_In_Style() { diff --git a/src/Uno.UI/DataBinding/BindingExpression.cs b/src/Uno.UI/DataBinding/BindingExpression.cs index 86ebbce76f19..e7656534d275 100644 --- a/src/Uno.UI/DataBinding/BindingExpression.cs +++ b/src/Uno.UI/DataBinding/BindingExpression.cs @@ -138,7 +138,11 @@ Binding binding } ApplyExplicitSource(); - ApplyElementName(); + } + + internal void OnAttach() + { + ApplyElementName(isOnAttach: true); } private ManagedWeakReference GetWeakDataContext() @@ -326,37 +330,82 @@ private UpdateSourceTrigger ResolveUpdateSourceTrigger() } } - internal void ApplyElementName() + private void TargetLoaded(object sender, RoutedEventArgs args) + { + var fe = (FrameworkElement)sender; + fe.Loaded -= TargetLoaded; + if (_disposed) + { + return; + } + + TryGetElementName(out _, out var elementName); + if (elementName is not null) + { + // TODO: This should also lookup the NameScope of TemplatedParent. + var target = NameScope.FindInNamescopes(_view?.Target as DependencyObject, elementName); + ExplicitSource = target; + ApplyExplicitSource(); + } + } + + private void TryGetElementName(out ElementNameSubject subject, out string elementName) { if (ParentBinding.ElementName is ElementNameSubject elementNameSubject) { + subject = elementNameSubject; + elementName = subject.Name; + } + else + { + subject = null; + elementName = ParentBinding.ElementName as string; + } + } - if (elementNameSubject.IsLoadTimeBound) + internal void ApplyElementName(bool isOnAttach = false) + { + TryGetElementName(out var elementNameSubject, out var elementName); + if (elementNameSubject is { IsLoadTimeBound: false }) + { + void applySource() { - // ElementName references in outer scopes will be resolvable on loading - var target = NameScope.FindInNamescopes(_view?.Target as DependencyObject, elementNameSubject.Name); - ExplicitSource = target; + ExplicitSource = elementNameSubject.ElementInstance; ApplyExplicitSource(); } + + // The element name instance may already have been + // set, in relation to the declaration order in the xaml file. + if (elementNameSubject.ElementInstance == null) + { + elementNameSubject + .ElementInstanceChanged += (s, elementNameInstance) => applySource(); + } + else + { + applySource(); + } + } + else if (elementName is not null && _view?.Target is DependencyObject dependencyObject) + { + // ElementName references in outer scopes will be resolvable on loading + // TODO: This should also lookup the NameScope of TemplatedParent. + var target = NameScope.FindInNamescopes(dependencyObject, elementName); + if (target is null && isOnAttach && dependencyObject is FrameworkElement fe1 && !fe1.IsInLiveTree) + { + fe1.Loaded += TargetLoaded; + } else { - void applySource() + if (target is not null && dependencyObject is FrameworkElement fe2) { - ExplicitSource = elementNameSubject.ElementInstance; - ApplyExplicitSource(); + // If the binding was re-evaluated after we subscribed to Loaded but + // before Loaded was actually fired, then we need to unsubscribe. + fe2.Loaded -= TargetLoaded; } - // The element name instance may already have been - // set, in relation to the declaration order in the xaml file. - if (elementNameSubject.ElementInstance == null) - { - elementNameSubject - .ElementInstanceChanged += (s, elementNameInstance) => applySource(); - } - else - { - applySource(); - } + ExplicitSource = target; + ApplyExplicitSource(); } } } diff --git a/src/Uno.UI/UI/Xaml/Controls/CalendarView/CalendarViewBaseItem.cs b/src/Uno.UI/UI/Xaml/Controls/CalendarView/CalendarViewBaseItem.cs index aff8aa16d901..0e2d055fa4d2 100644 --- a/src/Uno.UI/UI/Xaml/Controls/CalendarView/CalendarViewBaseItem.cs +++ b/src/Uno.UI/UI/Xaml/Controls/CalendarView/CalendarViewBaseItem.cs @@ -9,6 +9,8 @@ using CCalendarViewBaseItemChrome = Microsoft.UI.Xaml.Controls.CalendarViewBaseItem; using DateTime = Windows.Foundation.WindowsFoundationDateTime; using Uno.UI.Xaml; +using Microsoft.UI.Xaml.Markup; + #if HAS_UNO_WINUI using Microsoft.UI.Input; @@ -158,10 +160,10 @@ private protected override void OnIsEnabledChanged(IsEnabledChangedEventArgs pAr #if !__NETSTD_REFERENCE__ #if UNO_HAS_ENHANCED_LIFECYCLE - internal override void Enter(EnterParams @params, int depth) + internal override void Enter(INameScope nameScope, EnterParams @params, int depth) { //base.EnterImpl(bLive, bSkipNameRegistration, bCoercedIsEnabled, bUseLayoutRounding); - base.Enter(@params, depth); + base.Enter(nameScope, @params, depth); #else private void EnterImpl() { diff --git a/src/Uno.UI/UI/Xaml/Controls/ContentPresenter/ContentPresenter.cs b/src/Uno.UI/UI/Xaml/Controls/ContentPresenter/ContentPresenter.cs index 319af21cf584..079bd1d8f257 100644 --- a/src/Uno.UI/UI/Xaml/Controls/ContentPresenter/ContentPresenter.cs +++ b/src/Uno.UI/UI/Xaml/Controls/ContentPresenter/ContentPresenter.cs @@ -835,9 +835,9 @@ private void CleanupView(View previousValue) } #if UNO_HAS_ENHANCED_LIFECYCLE - internal override void Enter(EnterParams @params, int depth) + internal override void Enter(INameScope nameScope, EnterParams @params, int depth) { - base.Enter(@params, depth); + base.Enter(nameScope, @params, depth); if (ResetDataContextOnFirstLoad() || ContentTemplateRoot == null) { diff --git a/src/Uno.UI/UI/Xaml/Controls/FlipView/FlipView.managed.cs b/src/Uno.UI/UI/Xaml/Controls/FlipView/FlipView.managed.cs index c80a7dc0b0ca..516266671e3e 100644 --- a/src/Uno.UI/UI/Xaml/Controls/FlipView/FlipView.managed.cs +++ b/src/Uno.UI/UI/Xaml/Controls/FlipView/FlipView.managed.cs @@ -23,6 +23,8 @@ using DirectUI; using Uno.UI.Xaml.Core; using Uno.UI.Xaml.Input; +using Microsoft.UI.Xaml.Markup; + #if HAS_UNO_WINUI using Microsoft.UI.Input; @@ -120,16 +122,16 @@ partial void InitializePartial() m_itemsAreSized = false; } - internal override void Enter(EnterParams @params, int depth) + internal override void Enter(INameScope nameScope, EnterParams @params, int depth) { - base.Enter(@params, depth); + base.Enter(nameScope, @params, depth); HookTemplate(); } - internal override void Leave(LeaveParams @params) + internal override void Leave(INameScope nameScope, LeaveParams @params) { - base.Leave(@params); + base.Leave(nameScope, @params); UnhookTemplate(); } diff --git a/src/Uno.UI/UI/Xaml/DependencyPropertyDetailsCollection.Bindings.cs b/src/Uno.UI/UI/Xaml/DependencyPropertyDetailsCollection.Bindings.cs index 5ebb8d4dda78..f1052274c8bd 100644 --- a/src/Uno.UI/UI/Xaml/DependencyPropertyDetailsCollection.Bindings.cs +++ b/src/Uno.UI/UI/Xaml/DependencyPropertyDetailsCollection.Bindings.cs @@ -137,6 +137,7 @@ internal void SetBinding(DependencyProperty dependencyProperty, Binding binding, binding: binding ); + bindingExpression.OnAttach(); details.SetBinding(bindingExpression); if (Equals(binding.RelativeSource, RelativeSource.TemplatedParent)) diff --git a/src/Uno.UI/UI/Xaml/FrameworkElement.Layout.crossruntime.cs b/src/Uno.UI/UI/Xaml/FrameworkElement.Layout.crossruntime.cs index 05f16b19fd15..29a4a29b6908 100644 --- a/src/Uno.UI/UI/Xaml/FrameworkElement.Layout.crossruntime.cs +++ b/src/Uno.UI/UI/Xaml/FrameworkElement.Layout.crossruntime.cs @@ -17,6 +17,8 @@ using Uno.UI.Xaml.Core; using Uno.UI.Xaml.Core.Scaling; using Uno.UI.Extensions; +using Microsoft.UI.Xaml.Media; +using Microsoft.UI.Xaml.Markup; namespace Microsoft.UI.Xaml { @@ -900,8 +902,43 @@ private void ArrangeNative(Point offset, Rect? clippedFrame) #endif } - internal override void Enter(EnterParams @params, int depth) + private void AdjustNameScope(ref INameScope? nameScope) { + if (NameScope.GetNameScope(this) is { } currentNameScope) + { + nameScope = currentNameScope; + } + } + + internal override void Enter(INameScope? nameScope, EnterParams @params, int depth) + { + // This should happen regardless of whether Name is null or not. + // What we need here is that once we find an element being entered that + // has its own NameScope, then we start using this namescope and use it for entering its children. + // For example, elements in a template where the template root will have its own NameScope. + AdjustNameScope(ref nameScope); + + if (nameScope is not null) + { + var name = this.Name; + + if (!string.IsNullOrEmpty(name) && nameScope.FindName(name) is null) + { + // Ideally, we shouldn't be checking for null FindName. + // But it's currently needed due to the way we + // register names in namescopes which don't yet match WinUI completely. + // Right now, XAML generator will do RegisterName calls, then, when we + // enter the visual tree, we will be trying to register the name again. + // However, registering in Enter is also very necessary in case C# is used + // instead of XAML (be it "regular" C# code or C# Markup). + // So, we need to have RegisterName here. + // The RegisterName calls in XAML generator should probably happen via something + // similar to WinUI's PostParseRegisterNames which will basically just do an "Enter" with + // isLive being false. Then, the Enter happening in VisualTree.AddRoot should skip name registration. + nameScope.RegisterName(name, this); + } + } + // The way this works on WinUI is that when an element enters the visual tree, all values // of properties that are marked with MetaDataPropertyInfoFlags::IsSparse and MetaDataPropertyInfoFlags::IsVisualTreeProperty // are entered as well. @@ -913,12 +950,12 @@ internal override void Enter(EnterParams @params, int depth) if (resource is FrameworkElement resourceAsUIElement) { resourceAsUIElement.XamlRoot = XamlRoot; - resourceAsUIElement.Enter(@params, int.MinValue); + resourceAsUIElement.Enter(nameScope, @params, int.MinValue); } } } - base.Enter(@params, depth); + base.Enter(nameScope, @params, depth); if (@params.IsLive) { @@ -931,8 +968,24 @@ internal override void Enter(EnterParams @params, int depth) m_firedLoadingEvent = false; } - internal override void Leave(LeaveParams @params) + internal override void Leave(INameScope? nameScope, LeaveParams @params) { + // This should happen regardless of whether Name is null or not. + // What we need here is that once we find an element leaving that + // has its own NameScope, then we start using this namescope and use it for leaving its children. + // For example, elements in a template where the template root will have its own NameScope. + AdjustNameScope(ref nameScope); + + if (nameScope is not null) + { + var name = this.Name; + + if (!string.IsNullOrEmpty(name)) + { + nameScope.UnregisterName(name); + } + } + // The way this works on WinUI is that when an element enters the visual tree, all values // of properties that are marked with MetaDataPropertyInfoFlags::IsSparse and MetaDataPropertyInfoFlags::IsVisualTreeProperty // are entered as well. @@ -943,12 +996,12 @@ internal override void Leave(LeaveParams @params) { if (resource is FrameworkElement resourceAsUIElement) { - resourceAsUIElement.Leave(@params); + resourceAsUIElement.Leave(nameScope, @params); } } } - base.Leave(@params); + base.Leave(nameScope, @params); ReconfigureViewportPropagation(isLeavingTree: true); } diff --git a/src/Uno.UI/UI/Xaml/Internal/VisualTree.cs b/src/Uno.UI/UI/Xaml/Internal/VisualTree.cs index 18b3be3cd9ca..85e96c72629b 100644 --- a/src/Uno.UI/UI/Xaml/Internal/VisualTree.cs +++ b/src/Uno.UI/UI/Xaml/Internal/VisualTree.cs @@ -435,6 +435,13 @@ private void AddRoot(UIElement? root) //TODO Uno: The logic here is more complex in WinUI, //setting the namescope owner. Not needed currently. + // While XAML generator should be setting NameScope, this is to support cases where + // C# is used instead of XAML (it could be "regular" pure C#, or C# Markup) + if (root == PublicRootVisual && NameScope.GetNameScope(root) is null) + { + NameScope.SetNameScope(root, new NameScope() { Owner = root }); + } + #if UNO_HAS_ENHANCED_LIFECYCLE if (IsMainVisualTree()) { @@ -457,7 +464,7 @@ private void AddRoot(UIElement? root) // In WinUI, this is called only under IsMainVisualTree condition. // This might be needed for now in Uno because RootVisual does not *yet* have XamlIslandRootCollection - root.Enter(enterParams, 0); + root.Enter(NameScope.GetNameScope(root), enterParams, 0); #endif } } diff --git a/src/Uno.UI/UI/Xaml/ResourceDictionary.cs b/src/Uno.UI/UI/Xaml/ResourceDictionary.cs index ab58ff9a08cd..171ebd0be490 100644 --- a/src/Uno.UI/UI/Xaml/ResourceDictionary.cs +++ b/src/Uno.UI/UI/Xaml/ResourceDictionary.cs @@ -140,7 +140,7 @@ public bool Remove(object key) if (value is FrameworkElement fe) { #if UNO_HAS_ENHANCED_LIFECYCLE - fe.Leave(new LeaveParams()); + fe.Leave(UIElement.FindNameScope(fe), new LeaveParams()); #else fe.PerformOnUnloaded(isFromResources: true); #endif diff --git a/src/Uno.UI/UI/Xaml/UIElement.crossruntime.cs b/src/Uno.UI/UI/Xaml/UIElement.crossruntime.cs index caae2f2caf88..1952b4205a3f 100644 --- a/src/Uno.UI/UI/Xaml/UIElement.crossruntime.cs +++ b/src/Uno.UI/UI/Xaml/UIElement.crossruntime.cs @@ -18,6 +18,7 @@ using System.Reflection; using Uno.Core.Comparison; +using Microsoft.UI.Xaml.Markup; namespace Microsoft.UI.Xaml { @@ -109,7 +110,7 @@ private void OnChildRemoved(UIElement child) #if UNO_HAS_ENHANCED_LIFECYCLE var leaveParams = new LeaveParams(IsActiveInVisualTree); - child.Leave(leaveParams); + child.Leave(FindNameScope(this), leaveParams); #endif } @@ -117,12 +118,31 @@ internal Point GetPosition(Point position, UIElement relativeTo) => TransformToVisual(relativeTo).TransformPoint(position); #if UNO_HAS_ENHANCED_LIFECYCLE + internal static INameScope FindNameScope(DependencyObject dependencyObject) + { + // In many cases, this will not traverse the entire visual tree, as the NameScope is often + // present on each element as the DP is inherited. However, it can happen that the DP inheritance + // didn't yet happen, in which case we'll traverse the tree up. + var current = dependencyObject; + while (current is not null) + { + if (NameScope.GetNameScope(current) is { } nameScope) + { + return nameScope; + } + + current = VisualTreeHelper.GetParent(current); + } + + return null; + } + private void ChildEnter(UIElement child, EnterParams @params) { // Uno TODO: WinUI has much more complex logic than this. if (@params.IsLive) { - child.Enter(@params, this.Depth + 1); + child.Enter(FindNameScope(this), @params, this.Depth + 1); } } #endif diff --git a/src/Uno.UI/UI/Xaml/UIElement.mux.cs b/src/Uno.UI/UI/Xaml/UIElement.mux.cs index f2415bd8bfa8..8781c5ee51c1 100644 --- a/src/Uno.UI/UI/Xaml/UIElement.mux.cs +++ b/src/Uno.UI/UI/Xaml/UIElement.mux.cs @@ -16,6 +16,7 @@ using Microsoft.UI.Xaml.Input; using Microsoft.UI.Xaml.Media; using static Microsoft/* UWP don't rename */.UI.Xaml.Controls._Tracing; +using Microsoft.UI.Xaml.Markup; namespace Microsoft.UI.Xaml { @@ -617,7 +618,7 @@ internal Rect GetGlobalBoundsWithOptions(bool ignoreClipping, bool ignoreClippin #if UNO_HAS_ENHANCED_LIFECYCLE // Doesn't exactly match WinUI code. - internal virtual void Enter(EnterParams @params, int depth) + internal virtual void Enter(INameScope? nameScope, EnterParams @params, int depth) { Depth = depth; @@ -638,7 +639,7 @@ internal virtual void Enter(EnterParams @params, int depth) continue; } - child.Enter(@params, depth + 1); + child.Enter(nameScope, @params, depth + 1); } if (@params.IsLive) @@ -651,7 +652,7 @@ internal virtual void Enter(EnterParams @params, int depth) SetLayoutFlags(LayoutFlag.MeasureDirty | LayoutFlag.ArrangeDirty); } - internal virtual void Leave(LeaveParams @params) + internal virtual void Leave(INameScope? nameScope, LeaveParams @params) { foreach (var child in _children) { @@ -665,7 +666,7 @@ internal virtual void Leave(LeaveParams @params) continue; } - child.Leave(@params); + child.Leave(nameScope, @params); } if (IsActiveInVisualTree)