Skip to content

Commit

Permalink
fix: Adjust generation for x:Bind events in x:Load context
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Mar 29, 2021
1 parent af614a0 commit 0938736
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,6 @@ private void BuildCompiledBindingsInitializer(IndentedStringBuilder writer, stri
}

BuildComponentResouceBindingUpdates(writer);
BuildxBindEventHandlerInitializers(writer);
}

writer.AppendLineInvariant(";");
Expand All @@ -828,14 +827,14 @@ private void BuildCompiledBindingsInitializerForTemplate(IIndentedStringBuilder
}
}

private void BuildxBindEventHandlerInitializers(IIndentedStringBuilder writer)
private void BuildxBindEventHandlerInitializers(IIndentedStringBuilder writer, string prefix = "")
{
foreach (var xBindEventHandler in CurrentScope.xBindEventsHandlers)
{
writer.AppendLineInvariant($"{xBindEventHandler.Name}?.Invoke();");
writer.AppendLineInvariant($"{prefix}{xBindEventHandler.Name}?.Invoke();");

// Only needs to happen once per visual tree creation
writer.AppendLineInvariant($"{xBindEventHandler.Name} = null;");
writer.AppendLineInvariant($"{prefix}{xBindEventHandler.Name} = null;");
}
}

Expand Down Expand Up @@ -913,6 +912,8 @@ private void BuildCompiledBindings(IndentedStringBuilder writer, string classNam

writer.AppendLineInvariant($"owner._component_{i}{wrapInstance}.ApplyXBind();");
}

BuildxBindEventHandlerInitializers(writer, "owner.");
}
}
using (writer.BlockInvariant($"void {bindingsInterfaceName}.StopTracking()")) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
x:FieldModifier="public"
Checked="{x:Bind ViewModel.OnCheckedRaised}"
Unchecked="{x:Bind ViewModel.OnUncheckedRaised}"/>
<CheckBox x:Name="myCheckBox2"
x:FieldModifier="public"
Checked="{x:Bind ViewModel.OnCheckedRaised}"
Unchecked="{x:Bind ViewModel.OnUncheckedRaised}"/>
</Grid>
</DataTemplate>
</ContentControl.ContentTemplate>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Page x:Class="Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests.Controls.Binding_xLoad_Event"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests.Controls"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid x:Name="rootGrid"
x:FieldModifier="public"
x:Load="{x:Bind TopLevelVisiblity, Mode=OneWay}">
<CheckBox x:Name="myCheckBox"
x:FieldModifier="public"
Checked="{x:Bind ViewModel.OnCheckedRaised}"
Unchecked="{x:Bind ViewModel.OnUncheckedRaised}"/>
</Grid>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
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 Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests.Controls
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class Binding_xLoad_Event : Page
{
public Binding_xLoad_Event()
{
this.InitializeComponent();
}

public bool TopLevelVisiblity
{
get { return (bool)GetValue(TopLevelVisiblityProperty); }
set { SetValue(TopLevelVisiblityProperty, value); }
}

// Using a DependencyProperty as the backing store for TopLevelVisiblity. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TopLevelVisiblityProperty =
DependencyProperty.Register("TopLevelVisiblity", typeof(bool), typeof(Binding_xLoad_Event), new PropertyMetadata(false));

public Binding_xLoad_Event_ViewModel ViewModel { get; } = new Binding_xLoad_Event_ViewModel();
}

public class Binding_xLoad_Event_ViewModel
{
public int CheckedRaised { get; private set; }
public int UncheckedRaised { get; private set; }

public void OnCheckedRaised() => CheckedRaised++;

public void OnUncheckedRaised(object sender, RoutedEventArgs args) => UncheckedRaised++;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,21 @@ public void When_Event_Nested_DataTemplate()

Assert.AreEqual(1, dc.ViewModel.CheckedRaised);
Assert.AreEqual(1, dc.ViewModel.UncheckedRaised);

var checkBox2 = SUT.FindName("myCheckBox2") as CheckBox;

Assert.AreEqual(1, dc.ViewModel.CheckedRaised);
Assert.AreEqual(1, dc.ViewModel.UncheckedRaised);

checkBox2.IsChecked = true;

Assert.AreEqual(2, dc.ViewModel.CheckedRaised);
Assert.AreEqual(1, dc.ViewModel.UncheckedRaised);

checkBox2.IsChecked = false;

Assert.AreEqual(2, dc.ViewModel.CheckedRaised);
Assert.AreEqual(2, dc.ViewModel.UncheckedRaised);
}

[TestMethod]
Expand Down Expand Up @@ -803,6 +818,62 @@ public void When_xLoad_DataTemplate()
Assert.AreEqual(Visibility.Collapsed, topLevelContent.Visibility);
}

[TestMethod]
public void When_xLoad_Event()
{
var SUT = new Binding_xLoad_Event();

SUT.ForceLoaded();

Assert.IsNull(SUT.myCheckBox);
Assert.IsNull(SUT.rootGrid);

SUT.TopLevelVisiblity = true;

Assert.IsNotNull(SUT.myCheckBox);
Assert.IsNotNull(SUT.rootGrid);

var checkBox = SUT.FindName("myCheckBox") as CheckBox;

Assert.AreEqual(0, SUT.ViewModel.CheckedRaised);
Assert.AreEqual(0, SUT.ViewModel.UncheckedRaised);

checkBox.IsChecked = true;

Assert.AreEqual(1, SUT.ViewModel.CheckedRaised);
Assert.AreEqual(0, SUT.ViewModel.UncheckedRaised);

checkBox.IsChecked = false;

Assert.AreEqual(1, SUT.ViewModel.CheckedRaised);
Assert.AreEqual(1, SUT.ViewModel.UncheckedRaised);

SUT.TopLevelVisiblity = false;

// After reload
SUT.TopLevelVisiblity = true;

Assert.IsNotNull(SUT.myCheckBox);
Assert.IsNotNull(SUT.rootGrid);

var checkBox2 = SUT.FindName("myCheckBox") as CheckBox;

Assert.AreNotEqual(checkBox, checkBox2);

Assert.AreEqual(1, SUT.ViewModel.CheckedRaised);
Assert.AreEqual(1, SUT.ViewModel.UncheckedRaised);

checkBox2.IsChecked = true;

Assert.AreEqual(2, SUT.ViewModel.CheckedRaised);
Assert.AreEqual(1, SUT.ViewModel.UncheckedRaised);

checkBox2.IsChecked = false;

Assert.AreEqual(2, SUT.ViewModel.CheckedRaised);
Assert.AreEqual(2, SUT.ViewModel.UncheckedRaised);
}

[TestMethod]
public void When_PropertyChanged_Empty()
{
Expand Down

0 comments on commit 0938736

Please sign in to comment.