Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2c22e8c
Add support for x:Bind functions
jeromelaban Dec 23, 2019
2b268c7
Adjust parser
jeromelaban Dec 23, 2019
4204f47
Fix XamlMember equality does not use DeclaringType
jeromelaban Dec 23, 2019
2f22365
Update binding paths suoport for private members
jeromelaban Dec 23, 2019
490a9b0
Add x:Bind functions tests
jeromelaban Dec 23, 2019
ed585f0
Add more tests, x:False and x:True support
jeromelaban Dec 23, 2019
baff56b
Support DataTemplate for x:Bind
jeromelaban Dec 23, 2019
1b2bf7a
Null coalescing support
jeromelaban Jan 3, 2020
15e73c3
Adjust initial oneway/twoway x:Bind
jeromelaban Jan 5, 2020
232b821
Move x:Bind control samples to folder
jeromelaban Jan 6, 2020
223fecd
Validate PropertyChanged registrations
jeromelaban Jan 6, 2020
0ccdc42
Adjust for missing class in XAML validation
jeromelaban Jan 6, 2020
0c1003b
Adjust support for FallbackValue
jeromelaban Jan 6, 2020
b4356ae
Add x:Bind implementation documentation
jeromelaban Jan 6, 2020
6236a86
fix sys:String.Format sample
jeromelaban Jan 6, 2020
b15dafb
Adjust x:Bind test values
jeromelaban Jan 6, 2020
9466ddb
Add documentation for supported x:Bind features
jeromelaban Jan 6, 2020
71bd3bc
Adjust for x:Bind Discrepancies
jeromelaban Jan 6, 2020
34d381b
Update supported features
jeromelaban Jan 6, 2020
7851aaf
Add missing project for wasm solution filter
jeromelaban Jan 6, 2020
0e81682
Add x:Bind functions UI Test
jeromelaban Jan 6, 2020
06f40a4
Merge branch 'master' into dev/jela/xbind
jeromelaban Jan 6, 2020
cd63908
Merge branch 'master' into dev/jela/xbind
mergify[bot] Jan 7, 2020
eeda2ef
Merge branch 'master' into dev/jela/xbind
mergify[bot] Jan 7, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions Doc/articles/features/windows-ui-xaml-xbind.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Uno Support for x:Bind

Uno supports the [`x:Bind`](https://docs.microsoft.com/en-us/windows/uwp/xaml-platform/x-bind-markup-extension) WinUI feature, which gives the ability the bind to normal fields and properties, static classes fields, and functions with multiple parameters.

# Examples
- Properties
- Page or control property
```xaml
<TextBlock Text="{x:Bind MyProperty}" />
```
- Member function
```xaml
<TextBlock Text="{x:Bind MyProperty.ToUpper()}" />
```
- Static types field or properties OneTime binding
```xaml
<TextBlock Text="{x:Bind local:StaticType.PropertyIntValue}" />
```
- OneWay local member function with multiple observable parameters
```xaml
<TextBlock Text="{x:Bind Multiply(slider1.Value, slider2.Value), Mode=OneWay}" />
```
- OneWay static class function with multiple observable parameters
```xaml
<TextBlock Text="{x:Bind local:StaticType.Add(slider1.Value, slider2.Value), Mode=OneWay}" />
```
- Literal boolean parameters (`x:True`, `x:False`)
```xaml
<TextBlock Text="{x:Bind BoolFunc(x:False)}" />
```
- Null parameter (`x:Null`)
```xaml
<TextBlock Text="{x:Bind TestString(x:Null)}" />
```
- Quote escaping
```xaml
<TextBlock Text="{x:Bind sys:String.Format('{0}, ^'{1}^'', InstanceProperty, StaticProperty)}" />
```
- Literal numeric value
```
<TextBlock Text="{x:Bind Add(InstanceProperty, 42.42)}" />
```

- Use of system functions (given `xmlns:sys="using:System"`):
- Single parameter formatting:
```xaml
<TextBlock Text="{x:Bind sys:String.Format('Formatted {0}', MyProperty), Mode=OneWay}" />
```
- Multi parameters formatting:
```xaml
<TextBlock Text="{x:Bind sys:String.Format(x:Null, 'slider1: {0}, slider2:{1}', slider1.Value, slider2.Value), Mode=OneWay}" />
```
- TimeParsing:
```xaml
<CalendarDatePicker Date="{x:Bind sys:DateTime.Parse(TextBlock1.Text)}" />
```

# Not supported
- BindBack: `<TextBlock Text="{x:Bind a.MyFunc(b), BindBack=a.MyFunc2, Mode=TwoWay}" />`
- Type casts
2 changes: 1 addition & 1 deletion doc/articles/supported-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
- Inheritance
- Mode
- Trigger
- x:Bind *(without phases and expressions)*
- x:Bind with support functions
- Converters
- Attached Properties binding
- RelativeSource.Self
Expand Down
2 changes: 2 additions & 0 deletions doc/articles/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@
href: features/working-with-themes.md
- name: Routed Events
href: features/routed-events.md
- name: x:Bind
href: features\windows-ui-xaml-xbind.md
- name: Shapes & Brushes
href: features/shapes-and-brushes.md
- name: Speech Recognition
Expand Down
72 changes: 72 additions & 0 deletions doc/articles/uno-development/Uno-UI-xBind-architecture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# x:Bind in Uno Platform



## Overview of the x:Bind feature in WinUI

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the detailed findings and implementation discussion 👍


> Note that this section is based on observations of the behavior of `x:Bind`

`x:Bind` markup extensions have originally been developed by Microsoft to provide enhanced Data Binding performance, where no reflection is needed to do so. Code is generated along with XAML files that contain controls and DataTemplates, in `.g.cs` and `.g.i.cs` files.

Those bindings differ in multiple ways with standard `Binding` markup:
- The default binding mode is `OneTime` (`OneWay` for `Binding`)
- For top level controls the data context is the control itself
- For `DataTemplate`, the data context is the `DataContext` of the root element
- They can reference static fields and properties
- They can accept member and static functions with multiple parameters
- Function parameters can be string or number literals, as well as `x:True`, `x:False` and `x:Null`
- Function parameters can be the update source of the whole function binding

The creation of that generated code is performed in two passes:
- The first pass where x:Bind markup is removed from an intermediate file, creating unique "connections"
- The second pass creates a set of classes and interfaces within the top level class, or a hidden class for Data Templates.

The generated code is accessible through a private member called `Bindings`, which offers the following interface:
```csharp
private interface IXBindUserControl_Bindings
{
void Initialize();
void Update();
void StopTracking();
}
```
The most commonly used member is `Update` which allows to refresh a `OneTime` binding to a newer value.

## Implementation in Uno Platform

Uno is interpreting the XAML in a very different way, when compared to WinUI. Uno generates code at compile for most of the document, not just for the bindings, where WinUI generates code for `x:Bind` and XBF for the rest of the XAML.

In order to obtain a sufficiently close implementation of `x:Bind` the first iteration in Uno was only relying on the `Binding` engine to perform bindings. The only difference was in the default DataContext used by the resulting `Binding` object (`this` for `x:Bind`, the `DataContext` property for `DataTemplate`).

However, in order to implement the functions portions of `x:Bind`, more code needed to be generated. To the ability to observe changes on multiple source paths, in both `INotifyPropertyChanged` types, as well as `DependencyProperty` instances, runtime reflection or `TypeMetadataProvider` based path observation is used.

When encountering an `x:Bind` markup, the Uno generator parses the expression to generate a C# compatible expression, then extracts the "update sources". At this point, code is generated to invoke Uno specific APIs `Uno.UI.Xaml.BindingHelper.SetBindingXBindProvider` that add more information to the `Binding` class with properties paths, the binding source, and the function to execute when the target property needs to be updated.

For a typical OneTime `x:Bind` implementation:

```xaml
<TextBlock Text="{x:Bind TypeProperty.Value, FallbackValue=42}" />
```

The code is similar to this:

```csharp
c18.SetBinding(
global::Windows.UI.Xaml.Controls.TextBlock.TextProperty,
new Windows.UI.Xaml.Data.Binding{
Mode = global::Windows.UI.Xaml.Data.BindingMode.OneWay,
}
.Apply(___b =>
global::Uno.UI.Xaml.BindingHelper.SetBindingXBindProvider(
___b, // The binding to update
this, // The DataContext
___ctx => Add(InstanceDP, MyxBindClassInstance.MyIntProperty), // the code to execute
new [] {"InstanceDP", "MyxBindClassInstance.MyIntProperty"} // The properties to observe
)
)
);
```

At this point of the implementation, the executed expression is not yet decomposed to provide finer support for `FallbackValue`, where any member of an observed path may be null, and generate a NullReferenceException. A more advanced implementation will do so and stop the evaluation when a path member is null. In any case, the fallback value is applied if an exception occurs when executing the binding function.

The binding engine can still detect an invalid path, and stop the execution when the path has more than two indirections.
57 changes: 0 additions & 57 deletions src/SamplesApp/SamplesApp.UITests/UnoSamples_Tests.XBind.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using SamplesApp.UITests.TestFramework;
using Uno.UITest.Helpers;
using Uno.UITest.Helpers.Queries;

namespace SamplesApp.UITests
{
[TestFixture]
public partial class UnoSamples_Tests : SampleControlUITestBase
{
[Test]
[AutoRetry]
public void XBind_Validation()
{
Run("Uno.UI.Samples.Content.UITests.XBind.XBind_Simple");

{
// Wait for the first textblock value, the rest of the values are set
// synchronously in the test
var tb = _app.Marked("textBlock1");
_app.WaitFor(() => tb.GetDependencyPropertyValue("Text")?.ToString() != null);

Assert.AreEqual("42", tb.GetDependencyPropertyValue("Text")?.ToString());
}
{
// Discrepancies between Wasm and iOS/Android
// var tb = _app.Marked("textBlock1_Counter");
// Assert.AreEqual("Should be 1: 1", tb.GetDependencyPropertyValue("Text")?.ToString());
}
{
var tb = _app.Marked("textBlock2");
Assert.AreEqual("Should be 42: 42", tb.GetDependencyPropertyValue("Text")?.ToString());
}
{
var tb = _app.Marked("textBlock3");
Assert.AreEqual("Should be 43: 43", tb.GetDependencyPropertyValue("Text")?.ToString());
}
{
var tb = _app.Marked("textBlock4");
Assert.AreEqual("Should be 44: 44", tb.GetDependencyPropertyValue("Text")?.ToString());
}
{
// Discrepancies between Wasm and iOS/Android
// var tb = _app.Marked("textBlock5");
// Assert.AreEqual("Should be 2: 2", tb.GetDependencyPropertyValue("Text")?.ToString());
}
{
var tb = _app.Marked("textBlock6");
Assert.AreEqual("Should be 43: 43", tb.GetDependencyPropertyValue("Text")?.ToString());
}
{
var tb = _app.Marked("textBlock7");
Assert.AreEqual("Should be 44: 44", tb.GetDependencyPropertyValue("Text")?.ToString());
}
}

[Test]
[AutoRetry]
public void XBind_Functions()
{
Run("UITests.Shared.Windows_UI_Xaml.xBindTests.xBind_Functions");

var _StaticPropertyIntValue = _app.Marked("_StaticPropertyIntValue");
Assert.AreEqual("42", _StaticPropertyIntValue.GetText());

var _StaticPropertyStringValue = _app.Marked("_StaticPropertyStringValue");
Assert.AreEqual("value 43", _StaticPropertyStringValue.GetText());

var _StaticPropertyStringValueFunction = _app.Marked("_StaticPropertyStringValueFunction");
Assert.AreEqual("VALUE 43", _StaticPropertyStringValueFunction.GetText());

var _MemberFunctionMultiple_Bind_OneWay = _app.Marked("_MemberFunctionMultiple_Bind_OneWay");
Assert.AreEqual("440", _MemberFunctionMultiple_Bind_OneWay.GetText());

var _SaticFunctionMultiple_Bind_OneWay = _app.Marked("_SaticFunctionMultiple_Bind_OneWay");
Assert.AreEqual("42", _SaticFunctionMultiple_Bind_OneWay.GetText());

var _SystemFunction_Bind_OneWay = _app.Marked("_SystemFunction_Bind_OneWay");
Assert.AreEqual("slider1: 20, slider2:22", _SystemFunction_Bind_OneWay.GetText());

var updateTemplateButton = _app.Marked("updateTemplateButton");

var _DataTemplate_MyProperty = _app.Marked("_DataTemplate_MyProperty");
Assert.AreEqual("Initial", _DataTemplate_MyProperty.GetText());

var _DataTemplate_MyProperty_Function = _app.Marked("_DataTemplate_MyProperty_Function");
Assert.AreEqual("INITIAL", _DataTemplate_MyProperty_Function.GetText());

var _DataTemplate_MyProperty_Formatted = _app.Marked("_DataTemplate_MyProperty_Formatted");
Assert.AreEqual("Formatted Initial", _DataTemplate_MyProperty_Formatted.GetText());

var _DataTemplate_MyProperty_Function_OneWay = _app.Marked("_DataTemplate_MyProperty_Function_OneWay");
Assert.AreEqual("INITIAL", _DataTemplate_MyProperty_Function_OneWay.GetText());

var _DataTemplate_MyProperty_Formatted_OneWay = _app.Marked("_DataTemplate_MyProperty_Formatted_OneWay");
Assert.AreEqual("Formatted Initial", _DataTemplate_MyProperty_Formatted_OneWay.GetText());

updateTemplateButton.Tap();

Assert.AreEqual("NEW UPDATE", _DataTemplate_MyProperty_Function_OneWay.GetText());
Assert.AreEqual("Formatted new update", _DataTemplate_MyProperty_Formatted_OneWay.GetText());
}
}
}
3 changes: 2 additions & 1 deletion src/SamplesApp/SamplesApp.Wasm/SamplesApp.Wasm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<UnoSkipUserControlsInVisualTree>false</UnoSkipUserControlsInVisualTree>

<IsUiAutomationMappingEnabled>true</IsUiAutomationMappingEnabled>
<UseUnoXamlParser>true</UseUnoXamlParser>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Debug'">
Expand Down Expand Up @@ -60,7 +61,7 @@
<PackageReference Include="Microsoft.Extensions.Logging.Filter" Version="1.1.1" />
<PackageReference Include="BenchmarkDotNet" Version="0.11.4-develop" />
<PackageReference Include="Uno.Wasm.WebSockets" />
<PackageReference Include="Uno.Wasm.Bootstrap"/>
<PackageReference Include="Uno.Wasm.Bootstrap" />
<DotNetCliToolReference Include="Uno.Wasm.Bootstrap.Cli" />
</ItemGroup>

Expand Down
7 changes: 7 additions & 0 deletions src/SamplesApp/UITests.Shared/UITests.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\xBindTests\xBind_Functions.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\BitmapIconTests\BitmapIcon_Generic.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -3068,6 +3072,9 @@
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\xBindTests\PhaseBinding_StartOne.xaml.cs">
<DependentUpon>PhaseBinding_StartOne.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml\xBindTests\xBind_Functions.xaml.cs">
<DependentUpon>xBind_Functions.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Windows_UI_Xaml_Controls\Button\Buttons_Native.xaml.cs">
<DependentUpon>Buttons_Native.xaml</DependentUpon>
</Compile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<Grid>
<StackPanel>
<TextBlock x:Uid="textBlock1" Text="{x:Bind MyValue}" />
<TextBlock>
<Run Text="Should be 2: " />
<TextBlock x:Uid="textBlock1_Counter">
<Run Text="Should be 1: " />
<Run Text="{x:Bind BindingCounter}" />
</TextBlock>
<TextBlock x:Uid="textBlock2">
Expand Down
Loading