Skip to content

Commit 0938736

Browse files
committed
fix: Adjust generation for x:Bind events in x:Load context
1 parent af614a0 commit 0938736

File tree

5 files changed

+150
-4
lines changed

5 files changed

+150
-4
lines changed

src/SourceGenerators/Uno.UI.SourceGenerators/XamlGenerator/XamlFileGenerator.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,6 @@ private void BuildCompiledBindingsInitializer(IndentedStringBuilder writer, stri
802802
}
803803

804804
BuildComponentResouceBindingUpdates(writer);
805-
BuildxBindEventHandlerInitializers(writer);
806805
}
807806

808807
writer.AppendLineInvariant(";");
@@ -828,14 +827,14 @@ private void BuildCompiledBindingsInitializerForTemplate(IIndentedStringBuilder
828827
}
829828
}
830829

831-
private void BuildxBindEventHandlerInitializers(IIndentedStringBuilder writer)
830+
private void BuildxBindEventHandlerInitializers(IIndentedStringBuilder writer, string prefix = "")
832831
{
833832
foreach (var xBindEventHandler in CurrentScope.xBindEventsHandlers)
834833
{
835-
writer.AppendLineInvariant($"{xBindEventHandler.Name}?.Invoke();");
834+
writer.AppendLineInvariant($"{prefix}{xBindEventHandler.Name}?.Invoke();");
836835

837836
// Only needs to happen once per visual tree creation
838-
writer.AppendLineInvariant($"{xBindEventHandler.Name} = null;");
837+
writer.AppendLineInvariant($"{prefix}{xBindEventHandler.Name} = null;");
839838
}
840839
}
841840

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

914913
writer.AppendLineInvariant($"owner._component_{i}{wrapInstance}.ApplyXBind();");
915914
}
915+
916+
BuildxBindEventHandlerInitializers(writer, "owner.");
916917
}
917918
}
918919
using (writer.BlockInvariant($"void {bindingsInterfaceName}.StopTracking()")) { }

src/Uno.UI.Tests/Windows_UI_Xaml_Data/xBindTests/Controls/Binding_Event_Nested_DataTemplate.xaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
x:FieldModifier="public"
1616
Checked="{x:Bind ViewModel.OnCheckedRaised}"
1717
Unchecked="{x:Bind ViewModel.OnUncheckedRaised}"/>
18+
<CheckBox x:Name="myCheckBox2"
19+
x:FieldModifier="public"
20+
Checked="{x:Bind ViewModel.OnCheckedRaised}"
21+
Unchecked="{x:Bind ViewModel.OnUncheckedRaised}"/>
1822
</Grid>
1923
</DataTemplate>
2024
</ContentControl.ContentTemplate>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Page x:Class="Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests.Controls.Binding_xLoad_Event"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
4+
xmlns:local="using:Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests.Controls"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
6+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
mc:Ignorable="d">
8+
9+
<Grid x:Name="rootGrid"
10+
x:FieldModifier="public"
11+
x:Load="{x:Bind TopLevelVisiblity, Mode=OneWay}">
12+
<CheckBox x:Name="myCheckBox"
13+
x:FieldModifier="public"
14+
Checked="{x:Bind ViewModel.OnCheckedRaised}"
15+
Unchecked="{x:Bind ViewModel.OnUncheckedRaised}"/>
16+
</Grid>
17+
</Page>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Runtime.InteropServices.WindowsRuntime;
7+
using Windows.Foundation;
8+
using Windows.Foundation.Collections;
9+
using Windows.UI.Xaml;
10+
using Windows.UI.Xaml.Controls;
11+
using Windows.UI.Xaml.Controls.Primitives;
12+
using Windows.UI.Xaml.Data;
13+
using Windows.UI.Xaml.Input;
14+
using Windows.UI.Xaml.Media;
15+
using Windows.UI.Xaml.Navigation;
16+
17+
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238
18+
19+
namespace Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests.Controls
20+
{
21+
/// <summary>
22+
/// An empty page that can be used on its own or navigated to within a Frame.
23+
/// </summary>
24+
public sealed partial class Binding_xLoad_Event : Page
25+
{
26+
public Binding_xLoad_Event()
27+
{
28+
this.InitializeComponent();
29+
}
30+
31+
public bool TopLevelVisiblity
32+
{
33+
get { return (bool)GetValue(TopLevelVisiblityProperty); }
34+
set { SetValue(TopLevelVisiblityProperty, value); }
35+
}
36+
37+
// Using a DependencyProperty as the backing store for TopLevelVisiblity. This enables animation, styling, binding, etc...
38+
public static readonly DependencyProperty TopLevelVisiblityProperty =
39+
DependencyProperty.Register("TopLevelVisiblity", typeof(bool), typeof(Binding_xLoad_Event), new PropertyMetadata(false));
40+
41+
public Binding_xLoad_Event_ViewModel ViewModel { get; } = new Binding_xLoad_Event_ViewModel();
42+
}
43+
44+
public class Binding_xLoad_Event_ViewModel
45+
{
46+
public int CheckedRaised { get; private set; }
47+
public int UncheckedRaised { get; private set; }
48+
49+
public void OnCheckedRaised() => CheckedRaised++;
50+
51+
public void OnUncheckedRaised(object sender, RoutedEventArgs args) => UncheckedRaised++;
52+
}
53+
}

src/Uno.UI.Tests/Windows_UI_Xaml_Data/xBindTests/Given_xBind_Binding.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,21 @@ public void When_Event_Nested_DataTemplate()
741741

742742
Assert.AreEqual(1, dc.ViewModel.CheckedRaised);
743743
Assert.AreEqual(1, dc.ViewModel.UncheckedRaised);
744+
745+
var checkBox2 = SUT.FindName("myCheckBox2") as CheckBox;
746+
747+
Assert.AreEqual(1, dc.ViewModel.CheckedRaised);
748+
Assert.AreEqual(1, dc.ViewModel.UncheckedRaised);
749+
750+
checkBox2.IsChecked = true;
751+
752+
Assert.AreEqual(2, dc.ViewModel.CheckedRaised);
753+
Assert.AreEqual(1, dc.ViewModel.UncheckedRaised);
754+
755+
checkBox2.IsChecked = false;
756+
757+
Assert.AreEqual(2, dc.ViewModel.CheckedRaised);
758+
Assert.AreEqual(2, dc.ViewModel.UncheckedRaised);
744759
}
745760

746761
[TestMethod]
@@ -803,6 +818,62 @@ public void When_xLoad_DataTemplate()
803818
Assert.AreEqual(Visibility.Collapsed, topLevelContent.Visibility);
804819
}
805820

821+
[TestMethod]
822+
public void When_xLoad_Event()
823+
{
824+
var SUT = new Binding_xLoad_Event();
825+
826+
SUT.ForceLoaded();
827+
828+
Assert.IsNull(SUT.myCheckBox);
829+
Assert.IsNull(SUT.rootGrid);
830+
831+
SUT.TopLevelVisiblity = true;
832+
833+
Assert.IsNotNull(SUT.myCheckBox);
834+
Assert.IsNotNull(SUT.rootGrid);
835+
836+
var checkBox = SUT.FindName("myCheckBox") as CheckBox;
837+
838+
Assert.AreEqual(0, SUT.ViewModel.CheckedRaised);
839+
Assert.AreEqual(0, SUT.ViewModel.UncheckedRaised);
840+
841+
checkBox.IsChecked = true;
842+
843+
Assert.AreEqual(1, SUT.ViewModel.CheckedRaised);
844+
Assert.AreEqual(0, SUT.ViewModel.UncheckedRaised);
845+
846+
checkBox.IsChecked = false;
847+
848+
Assert.AreEqual(1, SUT.ViewModel.CheckedRaised);
849+
Assert.AreEqual(1, SUT.ViewModel.UncheckedRaised);
850+
851+
SUT.TopLevelVisiblity = false;
852+
853+
// After reload
854+
SUT.TopLevelVisiblity = true;
855+
856+
Assert.IsNotNull(SUT.myCheckBox);
857+
Assert.IsNotNull(SUT.rootGrid);
858+
859+
var checkBox2 = SUT.FindName("myCheckBox") as CheckBox;
860+
861+
Assert.AreNotEqual(checkBox, checkBox2);
862+
863+
Assert.AreEqual(1, SUT.ViewModel.CheckedRaised);
864+
Assert.AreEqual(1, SUT.ViewModel.UncheckedRaised);
865+
866+
checkBox2.IsChecked = true;
867+
868+
Assert.AreEqual(2, SUT.ViewModel.CheckedRaised);
869+
Assert.AreEqual(1, SUT.ViewModel.UncheckedRaised);
870+
871+
checkBox2.IsChecked = false;
872+
873+
Assert.AreEqual(2, SUT.ViewModel.CheckedRaised);
874+
Assert.AreEqual(2, SUT.ViewModel.UncheckedRaised);
875+
}
876+
806877
[TestMethod]
807878
public void When_PropertyChanged_Empty()
808879
{

0 commit comments

Comments
 (0)