diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs b/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs index d8a76c9e3a7d..22bb19288838 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs @@ -355,6 +355,7 @@ private SourceText InnerGenerateFile() if (topLevelControl.Type.Name == "ResourceDictionary") { _isTopLevelDictionary = true; + _xClassName = FindClassName(topLevelControl); using (TrySetDefaultBindMode(topLevelControl)) { diff --git a/src/Uno.UI.Tests/App/App.xaml b/src/Uno.UI.Tests/App/App.xaml index d461fb3ee965..508d4071508f 100644 --- a/src/Uno.UI.Tests/App/App.xaml +++ b/src/Uno.UI.Tests/App/App.xaml @@ -6,6 +6,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:views="using:Uno.UI.Tests.App.Views" xmlns:local="using:Uno.UI.Tests.App.Xaml" + xmlns:xbind="using:Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests.Controls" mc:Ignorable="not_win"> @@ -31,6 +32,7 @@ + diff --git a/src/Uno.UI.Tests/Windows_UI_Xaml_Data/xBindTests/Controls/XBind_ResourceDictionary.xaml b/src/Uno.UI.Tests/Windows_UI_Xaml_Data/xBindTests/Controls/XBind_ResourceDictionary.xaml new file mode 100644 index 000000000000..a7d72305be0f --- /dev/null +++ b/src/Uno.UI.Tests/Windows_UI_Xaml_Data/xBindTests/Controls/XBind_ResourceDictionary.xaml @@ -0,0 +1,30 @@ + + + + + diff --git a/src/Uno.UI.Tests/Windows_UI_Xaml_Data/xBindTests/Controls/XBind_ResourceDictionary.xaml.cs b/src/Uno.UI.Tests/Windows_UI_Xaml_Data/xBindTests/Controls/XBind_ResourceDictionary.xaml.cs new file mode 100644 index 000000000000..ee2c63b548a0 --- /dev/null +++ b/src/Uno.UI.Tests/Windows_UI_Xaml_Data/xBindTests/Controls/XBind_ResourceDictionary.xaml.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices.WindowsRuntime; +using Windows.Foundation; +using Windows.Foundation.Collections; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Controls.Primitives; +using Windows.UI.Xaml.Data; +using Windows.UI.Xaml.Input; +using Windows.UI.Xaml.Media; +using Windows.UI.Xaml.Navigation; + +namespace Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests.Controls; +public sealed partial class XBind_ResourceDictionary : ResourceDictionary +{ + public XBind_ResourceDictionary() + { + this.InitializeComponent(); + } +} diff --git a/src/Uno.UI.Tests/Windows_UI_Xaml_Data/xBindTests/Controls/XBind_ResourceDictionary_Control.cs b/src/Uno.UI.Tests/Windows_UI_Xaml_Data/xBindTests/Controls/XBind_ResourceDictionary_Control.cs new file mode 100644 index 000000000000..b2fcede39860 --- /dev/null +++ b/src/Uno.UI.Tests/Windows_UI_Xaml_Data/xBindTests/Controls/XBind_ResourceDictionary_Control.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; + +namespace Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests.Controls; + +internal class XBind_ResourceDictionary_Control : Control +{ + /// + /// Creates a new instance of the class. + /// + public XBind_ResourceDictionary_Control() + { + this.DefaultStyleKey = typeof(XBind_ResourceDictionary_Control); + + // Allows directly using this control as the x:DataType in the template. + this.DataContext = this; + } + + public bool ElementLoadedInvoked { get; private set; } + + public void Element_Loaded(object sender, RoutedEventArgs args) + { + ElementLoadedInvoked = true; + } +} diff --git a/src/Uno.UI.Tests/Windows_UI_Xaml_Data/xBindTests/Given_xBind_Binding.cs b/src/Uno.UI.Tests/Windows_UI_Xaml_Data/xBindTests/Given_xBind_Binding.cs index 64deb0ecd5ae..a8076ac1fcdb 100644 --- a/src/Uno.UI.Tests/Windows_UI_Xaml_Data/xBindTests/Given_xBind_Binding.cs +++ b/src/Uno.UI.Tests/Windows_UI_Xaml_Data/xBindTests/Given_xBind_Binding.cs @@ -1461,6 +1461,15 @@ public void When_Indexer_Then_Property_Access_Update_Collection_Element() Assert.AreEqual("Updated2", SUT.tbDict2.Text); } + [TestMethod] + public void When_XBind_In_ResourceDictionary() + { + var SUT = new XBind_ResourceDictionary_Control(); + SUT.ForceLoaded(); + + Assert.IsTrue(SUT.ElementLoadedInvoked); + } + private async Task AssertIsNullAsync(Func getter, TimeSpan? timeout = null) where T : class { timeout ??= TimeSpan.FromSeconds(1);