Skip to content

Commit

Permalink
Merge pull request #18695 from unoplatform/dev/mazi/xbind-to-const
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund authored Nov 7, 2024
2 parents 46b7725 + 24d3b0c commit 0a31460
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public _View Build(object __ResourceOwner_1)
__fe.Loading += delegate
{
_component_0.UpdateResourceBindings();
_component_0.ApplyXBind();
}
;
}
Expand Down Expand Up @@ -261,6 +262,7 @@ public _View Build(object __ResourceOwner_1)
__fe.Loading += delegate
{
_component_0.UpdateResourceBindings();
_component_0.ApplyXBind();
}
;
}
Expand Down Expand Up @@ -350,6 +352,7 @@ public _View Build(object __ResourceOwner_1)
__fe.Loading += delegate
{
_component_0.UpdateResourceBindings();
_component_0.ApplyXBind();
}
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,7 @@ private void BuildCompiledBindingsInitializerForTemplate(IIndentedStringBuilder
using (writer.BlockInvariant($"__fe.Loading += delegate"))
{
BuildComponentResouceBindingUpdates(writer);
BuildXBindApply(writer);
BuildxBindEventHandlerInitializers(writer, CurrentScope.xBindEventsHandlers);
}
writer.AppendLineIndented(";");
Expand Down Expand Up @@ -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++)
Expand Down Expand Up @@ -4230,8 +4244,10 @@ 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)
Expand Down Expand Up @@ -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}))";
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/Uno.UI.RuntimeTests/Tests/BindingTests/BindingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
48 changes: 48 additions & 0 deletions src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstControl.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<Control
x:Class="Uno.UI.RuntimeTests.Tests.XBindConstControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Uno.UI.RuntimeTests.Tests"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">

<Control.Resources>
<ResourceDictionary>

<Style TargetType="local:XBindConstControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:XBindConstControl" >
<Grid>
<StackPanel
HorizontalAlignment="Center"
VerticalAlignment="Center">
<TextBlock Text="Grid in Control" HorizontalAlignment="Center" />
<Border
Width="100"
Height="100"
BorderBrush="Black"
BorderThickness="1"
Background="LightCoral" />
<TextBlock Text="Grid in Control with x:Bind" HorizontalAlignment="Center" />
<Border
Width="{x:Bind local:XBindConstControl.MyWidth}"
Height="{x:Bind local:XBindConstControl.MyHeight}"
x:Name="BoundBorder"
BorderBrush="Black"
BorderThickness="1"
Background="LawnGreen" />
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

</ResourceDictionary>
</Control.Resources>

</Control>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.UI.Xaml.Controls;

namespace Uno.UI.RuntimeTests.Tests;

public sealed partial class XBindConstControl : Control
{
private const double MyWidth = 200;
private 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;


}
}
29 changes: 29 additions & 0 deletions src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Page x:Class="Uno.UI.RuntimeTests.Tests.XBindConstPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Uno.UI.RuntimeTests.Tests"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ScrollViewer>
<Grid>
<StackPanel
HorizontalAlignment="Center"
VerticalAlignment="Center">
<TextBlock Text="Grid in Page" HorizontalAlignment="Center"/>
<Grid
Width="100"
Height="100"
BorderBrush="Black"
BorderThickness="1"
Background="LightCoral" />
<TextBlock Text="Grid in Page, with x:Bind" HorizontalAlignment="Center" />
<Border
Width="{x:Bind local:XBindConstPage.MyWidth}"
Height="{x:Bind local:XBindConstPage.MyHeight}"
x:Name="BoundBorder"
BorderBrush="Black"
BorderThickness="1"
Background="LawnGreen" />
</StackPanel>
</Grid>
</ScrollViewer>
</Page>
16 changes: 16 additions & 0 deletions src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindConstPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.UI.Xaml.Controls;

namespace Uno.UI.RuntimeTests.Tests;

public sealed partial class XBindConstPage : Page
{
private const double MyWidth = 200;
private const double MyHeight = 200;

public XBindConstPage()
{
this.InitializeComponent();
}

public Border XBoundBorder => BoundBorder;
}

0 comments on commit 0a31460

Please sign in to comment.