Skip to content

Commit

Permalink
fix(xBind): Add support {x:Null} syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Jun 2, 2021
1 parent 2b05467 commit 1a6c2fd
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
?? "";

Expand Down Expand Up @@ -3737,6 +3738,7 @@ string buildBindBack()
}
else
{
var originalRawFunction = rawFunction;
rawFunction = string.IsNullOrEmpty(rawFunction) ? "___ctx" : XBindExpressionParser.Rewrite("___tctx", rawFunction, IsStaticMember);

string buildBindBack()
Expand Down Expand Up @@ -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}))";
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/Uno.UI.Tests/Uno.UI.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,6 @@
<UpToDateCheckInput Include="**\*.xaml" Exclude="bin\**\*.xaml;obj\**\*.xaml" />
</ItemGroup>

<ItemGroup>
<UpToDateCheckInput Remove="ResourceLoader\Controls\When_Collection_And_InlineProperty.xaml" />
</ItemGroup>

<ItemGroup>
<None Update="ResourceLoader\Controls\When_Collection_And_InlineProperty.xaml">
<Generator>MSBuild:Compile</Generator>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<UserControl
x:Class="Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests.Controls.Binding_xNull"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests.Controls">
<Grid>
<TextBlock x:Name="tb01"
x:FieldModifier="public"
Text="{x:Bind Published.ToString('MMM d', {x:Null})}"/>
<TextBlock x:Name="tb02"
x:FieldModifier="public"
Text="{x:Bind MyMethod('MMM d', {x:Null})}"/>
<TextBlock x:Name="tb03"
x:FieldModifier="public"
Text="{x:Bind MyMethod('MMM d', x:Null)}"/>
</Grid>
</UserControl>
Original file line number Diff line number Diff line change
@@ -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 ?? "<null>");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <null>", SUT.tb02.Text);

Assert.IsNotNull(SUT.tb03);
Assert.AreEqual("MMM d <null>", SUT.tb03.Text);
}

private async Task AssertIsNullAsync<T>(Func<T> getter, TimeSpan? timeout = null)
{
var sw = Stopwatch.StartNew();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="testPage"
Width="42">
</UserControl>
4 changes: 4 additions & 0 deletions src/Uno.UI/UI/Xaml/Markup/Reader/XamlObjectBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 1a6c2fd

Please sign in to comment.