From 1a6c2fd4f2f463071e01c41a106dd62422158ba8 Mon Sep 17 00:00:00 2001 From: Jerome Laban Date: Tue, 18 May 2021 16:18:35 -0400 Subject: [PATCH] fix(xBind): Add support `{x:Null}` syntax --- .../XamlGenerator/XamlFileGenerator.cs | 4 ++- src/Uno.UI.Tests/Uno.UI.Tests.csproj | 4 --- .../xBindTests/Controls/Binding_xNull.xaml | 17 ++++++++++ .../xBindTests/Controls/Binding_xNull.xaml.cs | 34 +++++++++++++++++++ .../xBindTests/Given_xBind_Binding.cs | 17 ++++++++++ .../XamlReaderTests/Given_XamlReader.cs | 7 ++++ .../When_Brush_And_StringColor.xamltest | 5 +++ .../Xaml/Markup/Reader/XamlObjectBuilder.cs | 4 +++ 8 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 src/Uno.UI.Tests/Windows_UI_Xaml_Data/xBindTests/Controls/Binding_xNull.xaml create mode 100644 src/Uno.UI.Tests/Windows_UI_Xaml_Data/xBindTests/Controls/Binding_xNull.xaml.cs create mode 100644 src/Uno.UI.Tests/Windows_UI_Xaml_Markup/XamlReaderTests/When_Brush_And_StringColor.xamltest diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs b/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs index 5768d659eb1a..31578e147392 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs @@ -3664,6 +3664,7 @@ private string BuildXBindEvalFunction(XamlMemberDefinition member, XamlObjectDef rawFunction = rawFunction ?.Replace("x:False", "false") .Replace("x:True", "true") + .Replace("{x:Null}", "null") .Replace("x:Null", "null") ?? ""; @@ -3737,6 +3738,7 @@ string buildBindBack() } else { + var originalRawFunction = rawFunction; rawFunction = string.IsNullOrEmpty(rawFunction) ? "___ctx" : XBindExpressionParser.Rewrite("___tctx", rawFunction, IsStaticMember); string buildBindBack() @@ -3777,7 +3779,7 @@ string buildBindBack() } var bindFunction = $"___ctx is global::{_className.ns + "." + _className.className} ___tctx ? (object)({rawFunction}) : null"; - return $".Apply(___b => /*defaultBindMode{GetDefaultBindMode()}*/ global::Uno.UI.Xaml.BindingHelper.SetBindingXBindProvider(___b, this, ___ctx => {bindFunction}, {buildBindBack()} {pathsArray}))"; + return $".Apply(___b => /*defaultBindMode{GetDefaultBindMode()} {originalRawFunction}*/ global::Uno.UI.Xaml.BindingHelper.SetBindingXBindProvider(___b, this, ___ctx => {bindFunction}, {buildBindBack()} {pathsArray}))"; } } diff --git a/src/Uno.UI.Tests/Uno.UI.Tests.csproj b/src/Uno.UI.Tests/Uno.UI.Tests.csproj index f1cf888fa2e5..c58076145949 100644 --- a/src/Uno.UI.Tests/Uno.UI.Tests.csproj +++ b/src/Uno.UI.Tests/Uno.UI.Tests.csproj @@ -151,10 +151,6 @@ - - - - MSBuild:Compile diff --git a/src/Uno.UI.Tests/Windows_UI_Xaml_Data/xBindTests/Controls/Binding_xNull.xaml b/src/Uno.UI.Tests/Windows_UI_Xaml_Data/xBindTests/Controls/Binding_xNull.xaml new file mode 100644 index 000000000000..15f7f54f09f1 --- /dev/null +++ b/src/Uno.UI.Tests/Windows_UI_Xaml_Data/xBindTests/Controls/Binding_xNull.xaml @@ -0,0 +1,17 @@ + + + + + + + diff --git a/src/Uno.UI.Tests/Windows_UI_Xaml_Data/xBindTests/Controls/Binding_xNull.xaml.cs b/src/Uno.UI.Tests/Windows_UI_Xaml_Data/xBindTests/Controls/Binding_xNull.xaml.cs new file mode 100644 index 000000000000..32995508f4a8 --- /dev/null +++ b/src/Uno.UI.Tests/Windows_UI_Xaml_Data/xBindTests/Controls/Binding_xNull.xaml.cs @@ -0,0 +1,34 @@ +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; + +// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236 + +namespace Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests.Controls +{ + public sealed partial class Binding_xNull : UserControl + { + public Binding_xNull() + { + this.InitializeComponent(); + } + + public DateTime Published => new DateTime(2000, 01, 01); + + public string MyMethod(string myString, object myParam) + { + return myString + " " + (myParam ?? ""); + } + } +} 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 975c854bc2ee..005addbaa15a 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 @@ -1069,6 +1069,23 @@ public async Task When_xLoad_Setter_Order() Assert.AreEqual(4, SUT.ellipse.StrokeThickness); } + [TestMethod] + public async Task When_Binding_xNull() + { + var SUT = new Binding_xNull(); + + SUT.ForceLoaded(); + + Assert.IsNotNull(SUT.tb01); + Assert.AreEqual("Jan 1", SUT.tb01.Text); + + Assert.IsNotNull(SUT.tb02); + Assert.AreEqual("MMM d ", SUT.tb02.Text); + + Assert.IsNotNull(SUT.tb03); + Assert.AreEqual("MMM d ", SUT.tb03.Text); + } + private async Task AssertIsNullAsync(Func getter, TimeSpan? timeout = null) { var sw = Stopwatch.StartNew(); diff --git a/src/Uno.UI.Tests/Windows_UI_Xaml_Markup/XamlReaderTests/Given_XamlReader.cs b/src/Uno.UI.Tests/Windows_UI_Xaml_Markup/XamlReaderTests/Given_XamlReader.cs index 455bd4d8ec47..54b46e9373ee 100644 --- a/src/Uno.UI.Tests/Windows_UI_Xaml_Markup/XamlReaderTests/Given_XamlReader.cs +++ b/src/Uno.UI.Tests/Windows_UI_Xaml_Markup/XamlReaderTests/Given_XamlReader.cs @@ -733,6 +733,13 @@ public void When_StateTrigger_PropertyPath() var r = Windows.UI.Xaml.Markup.XamlReader.Load(s) as UserControl; } + [TestMethod] + public void When_Brush_And_StringColor() + { + var s = GetContent(nameof(When_Brush_And_StringColor)); + var r = Windows.UI.Xaml.Markup.XamlReader.Load(s) as ContentControl; + } + private string GetContent(string testName) { var assembly = this.GetType().Assembly; diff --git a/src/Uno.UI.Tests/Windows_UI_Xaml_Markup/XamlReaderTests/When_Brush_And_StringColor.xamltest b/src/Uno.UI.Tests/Windows_UI_Xaml_Markup/XamlReaderTests/When_Brush_And_StringColor.xamltest new file mode 100644 index 000000000000..60e7d0d8a4c7 --- /dev/null +++ b/src/Uno.UI.Tests/Windows_UI_Xaml_Markup/XamlReaderTests/When_Brush_And_StringColor.xamltest @@ -0,0 +1,5 @@ + + diff --git a/src/Uno.UI/UI/Xaml/Markup/Reader/XamlObjectBuilder.cs b/src/Uno.UI/UI/Xaml/Markup/Reader/XamlObjectBuilder.cs index 3a9d06ad42ad..9ed2672f6397 100644 --- a/src/Uno.UI/UI/Xaml/Markup/Reader/XamlObjectBuilder.cs +++ b/src/Uno.UI/UI/Xaml/Markup/Reader/XamlObjectBuilder.cs @@ -103,6 +103,10 @@ private object LoadObject(XamlObjectDefinition control) { return stringValue; } + else if (type == typeof(Media.Brush) && control.Members.Where(m => m.Member.Name == "_UnknownContent").FirstOrDefault()?.Value is string brushStringValue) + { + return XamlBindingHelper.ConvertValue(typeof(Media.Brush), brushStringValue); + } else { var instance = Activator.CreateInstance(type);