From 5529e7b51841733d14a7eae9b99cac99cab8073e Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Tue, 5 Nov 2024 13:51:49 +0100 Subject: [PATCH 1/6] fix: Apply x:Bind expressions in Resources --- .../XamlGenerator/XamlFileGenerator.cs | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs b/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs index 3ead4da769ea..454c9607d1de 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs @@ -1084,6 +1084,7 @@ private void BuildCompiledBindingsInitializerForTemplate(IIndentedStringBuilder using (writer.BlockInvariant($"__fe.Loading += delegate")) { BuildComponentResouceBindingUpdates(writer); + BuildXBindApply(writer); BuildxBindEventHandlerInitializers(writer, CurrentScope.xBindEventsHandlers); } writer.AppendLineIndented(";"); @@ -1117,6 +1118,19 @@ private void BuildComponentResouceBindingUpdates(IIndentedStringBuilder writer) } } + private void BuildXBindApply(IIndentedStringBuilder writer) + { + for (var i = 0; i < CurrentScope.Components.Count; i++) + { + var component = CurrentScope.Components[i]; + + if (HasXBindMarkupExtension(component.XamlObject) && IsDependencyObject(component.XamlObject)) + { + writer.AppendLineIndented($"{component.MemberName}.ApplyXBind();"); + } + } + } + private void BuildComponentFields(IIndentedStringBuilder writer) { for (var i = 0; i < CurrentScope.Components.Count; i++) @@ -4229,9 +4243,11 @@ private string BuildXBindEvalFunction(XamlMemberDefinition member, XamlObjectDef var modeMember = bindNode.Members.FirstOrDefault(m => m.Member.Name == "Mode")?.Value?.ToString() ?? GetDefaultBindMode(); var rawBindBack = bindNode.Members.FirstOrDefault(m => m.Member.Name == "BindBack")?.Value?.ToString(); + + var sourceInstance = CurrentResourceOwner is not null ? CurrentResourceOwnerName : "__that"; var applyBindingParameters = _isHotReloadEnabled - ? "__that, (___b, __that)" + ? $"{sourceInstance}, (___b, {sourceInstance})" : "___b"; if (isInsideDataTemplate) @@ -4396,7 +4412,7 @@ string buildBindBack() ? ", new [] {" + string.Join(", ", formattedPaths) + "}" : ""; - return $".BindingApply({applyBindingParameters} => /*defaultBindMode{GetDefaultBindMode()} {rawFunction}*/ global::Uno.UI.Xaml.BindingHelper.SetBindingXBindProvider(___b, __that, ___ctx => {bindFunction}, {buildBindBack()} {pathsArray}))"; + return $".BindingApply({applyBindingParameters} => /*defaultBindMode{GetDefaultBindMode()} {rawFunction}*/ global::Uno.UI.Xaml.BindingHelper.SetBindingXBindProvider(___b, {sourceInstance}, ___ctx => {bindFunction}, {buildBindBack()} {pathsArray}))"; } } From 49eebab5829b43ca2ca6038d83fddaf629020dcb Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Tue, 5 Nov 2024 13:52:16 +0100 Subject: [PATCH 2/6] test: XBind to const samples --- .../Tests/BindingTests/XBindConstControl.xaml | 48 +++++++++++++++++++ .../BindingTests/XBindConstControl.xaml.cs | 27 +++++++++++ .../Tests/BindingTests/XBindConstPage.xaml | 29 +++++++++++ .../Tests/BindingTests/XBindConstPage.xaml.cs | 16 +++++++ 4 files changed, 120 insertions(+) create mode 100644 src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstControl.xaml create mode 100644 src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstControl.xaml.cs create mode 100644 src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstPage.xaml create mode 100644 src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstPage.xaml.cs diff --git a/src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstControl.xaml b/src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstControl.xaml new file mode 100644 index 000000000000..676d88c1ab33 --- /dev/null +++ b/src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstControl.xaml @@ -0,0 +1,48 @@ + + + + + + + + + + + diff --git a/src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstControl.xaml.cs b/src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstControl.xaml.cs new file mode 100644 index 000000000000..9e632f8d83a3 --- /dev/null +++ b/src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstControl.xaml.cs @@ -0,0 +1,27 @@ +using Microsoft.UI.Xaml.Controls; + +namespace Uno.UI.RuntimeTests.Tests; + +public sealed partial class XBindConstControl : Control +{ + public const double MyWidth = 200; + public const double MyHeight = 200; + + public XBindConstControl() + { + DefaultStyleKey = typeof(XBindConstControl); + + this.InitializeComponent(); + } + + public Border XBoundBorder { get; set; } + + protected override void OnApplyTemplate() + { + base.OnApplyTemplate(); + + XBoundBorder = GetTemplateChild("BoundBorder") as Border; + + + } +} diff --git a/src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstPage.xaml b/src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstPage.xaml new file mode 100644 index 000000000000..c87d58e9364c --- /dev/null +++ b/src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstPage.xaml @@ -0,0 +1,29 @@ + + + + + + + + + + + + diff --git a/src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstPage.xaml.cs b/src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstPage.xaml.cs new file mode 100644 index 000000000000..29535df4653d --- /dev/null +++ b/src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstPage.xaml.cs @@ -0,0 +1,16 @@ +using Microsoft.UI.Xaml.Controls; + +namespace Uno.UI.RuntimeTests.Tests; + +public sealed partial class XBindConstPage : Page +{ + public const double MyWidth = 200; + public const double MyHeight = 200; + + public XBindConstPage() + { + this.InitializeComponent(); + } + + public Border XBoundBorder => BoundBorder; +} From d029c379c232b3a2661f5777fdf6b405f21d9eab Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Tue, 5 Nov 2024 13:55:41 +0100 Subject: [PATCH 3/6] test: Validate x:Bind is applied when used within a control template --- .../Tests/BindingTests/BindingTests.cs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/Uno.UI.RuntimeTests/Tests/BindingTests/BindingTests.cs b/src/Uno.UI.RuntimeTests/Tests/BindingTests/BindingTests.cs index 77b1a3fc8ba0..766866959400 100644 --- a/src/Uno.UI.RuntimeTests/Tests/BindingTests/BindingTests.cs +++ b/src/Uno.UI.RuntimeTests/Tests/BindingTests/BindingTests.cs @@ -128,4 +128,24 @@ public async Task When_FallbackValueThemeResource_WithDataContext() Assert.AreEqual(Microsoft.UI.Colors.Red, ((SolidColorBrush)myBtn.Foreground).Color); } + + [TestMethod] + public async Task When_XBind_To_Const_Page() + { + var SUT = new XBindConstPage(); + await UITestHelper.Load(SUT); + + Assert.AreEqual(200, SUT.XBoundBorder.ActualWidth); + Assert.AreEqual(200, SUT.XBoundBorder.ActualHeight); + } + + [TestMethod] + public async Task When_XBind_To_Const_Control_Template() + { + var SUT = new XBindConstControl(); + await UITestHelper.Load(SUT); + + Assert.AreEqual(200, SUT.XBoundBorder.ActualWidth); + Assert.AreEqual(200, SUT.XBoundBorder.ActualHeight); + } } From 92adec90e6f0377a22a3ef1997d6ba0e55322b6e Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Wed, 6 Nov 2024 13:10:38 +0100 Subject: [PATCH 4/6] chore: Adjust formatting --- .../Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs b/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs index 454c9607d1de..5f23b7778c7a 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs @@ -4243,7 +4243,7 @@ private string BuildXBindEvalFunction(XamlMemberDefinition member, XamlObjectDef var modeMember = bindNode.Members.FirstOrDefault(m => m.Member.Name == "Mode")?.Value?.ToString() ?? GetDefaultBindMode(); var rawBindBack = bindNode.Members.FirstOrDefault(m => m.Member.Name == "BindBack")?.Value?.ToString(); - + var sourceInstance = CurrentResourceOwner is not null ? CurrentResourceOwnerName : "__that"; var applyBindingParameters = _isHotReloadEnabled From ece9fded974aa3a50219bba53004605b59954855 Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Wed, 6 Nov 2024 17:19:37 +0100 Subject: [PATCH 5/6] chore: Adjust test --- ...or_MyResourceDictionary_92716e07ff456818f6d4125e055d4d57.cs | 3 +++ 1 file changed, 3 insertions(+) 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 a6e3e15cbe04..ee3191108d83 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 @@ -95,6 +95,7 @@ public _View Build(object __ResourceOwner_1) __fe.Loading += delegate { _component_0.UpdateResourceBindings(); + _component_0.ApplyXBind(); } ; } @@ -261,6 +262,7 @@ public _View Build(object __ResourceOwner_1) __fe.Loading += delegate { _component_0.UpdateResourceBindings(); + _component_0.ApplyXBind(); } ; } @@ -350,6 +352,7 @@ public _View Build(object __ResourceOwner_1) __fe.Loading += delegate { _component_0.UpdateResourceBindings(); + _component_0.ApplyXBind(); } ; } From 24d3b0cd72e45e38c17bfc7175c0724756dca0b6 Mon Sep 17 00:00:00 2001 From: Martin Zikmund Date: Thu, 7 Nov 2024 11:26:58 +0100 Subject: [PATCH 6/6] chore: Address comment --- .../Tests/BindingTests/XBindConstControl.xaml.cs | 4 ++-- .../Tests/BindingTests/XBindConstPage.xaml.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstControl.xaml.cs b/src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstControl.xaml.cs index 9e632f8d83a3..0ab167c8306b 100644 --- a/src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstControl.xaml.cs +++ b/src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstControl.xaml.cs @@ -4,8 +4,8 @@ namespace Uno.UI.RuntimeTests.Tests; public sealed partial class XBindConstControl : Control { - public const double MyWidth = 200; - public const double MyHeight = 200; + private const double MyWidth = 200; + private const double MyHeight = 200; public XBindConstControl() { diff --git a/src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstPage.xaml.cs b/src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstPage.xaml.cs index 29535df4653d..107fca0a7e32 100644 --- a/src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstPage.xaml.cs +++ b/src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstPage.xaml.cs @@ -4,8 +4,8 @@ namespace Uno.UI.RuntimeTests.Tests; public sealed partial class XBindConstPage : Page { - public const double MyWidth = 200; - public const double MyHeight = 200; + private const double MyWidth = 200; + private const double MyHeight = 200; public XBindConstPage() {