Skip to content

Commit

Permalink
fix: Fix sender argument in Window events and x:Bind in Window when i…
Browse files Browse the repository at this point in the history
…t's root of XAML
  • Loading branch information
Youssef1313 committed May 3, 2024
1 parent c548ca3 commit 9bd9c2e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,8 @@ private void BuildCompiledBindingsInitializer(IndentedStringBuilder writer, INam
IsType(_xClassName.Symbol, frameworkElementSymbol) // The current type may not have a base type as it is defined in XAML,
|| IsType(controlBaseType, frameworkElementSymbol); // so look at the control base type extracted from the XAML.

var isWindow = IsWindow(controlBaseType);

if (hasXBindExpressions || hasResourceExtensions)
{
var activator = _isHotReloadEnabled
Expand All @@ -1010,15 +1012,19 @@ private void BuildCompiledBindingsInitializer(IndentedStringBuilder writer, INam
writer.AppendLineIndented($"Bindings = {activator};");
}

if (isFrameworkElement && (hasXBindExpressions || hasResourceExtensions))
if ((isFrameworkElement || isWindow) && (hasXBindExpressions || hasResourceExtensions))
{
if (_isHotReloadEnabled)
{
// Attach the current context to itself to avoid having a closure in the lambda
writer.AppendLineIndented($"global::Uno.UI.Helpers.MarkupHelper.SetElementProperty(__that, \"owner\", __that);");
}

using (writer.BlockInvariant($"Loading += (s, e) => "))
var eventSubscription = isFrameworkElement
? "Loading += (s, e) =>"
: "Activated += (s, e) =>";

using (writer.BlockInvariant(eventSubscription))
{
if (_isHotReloadEnabled && _xClassName.Symbol != null)
{
Expand Down
19 changes: 19 additions & 0 deletions src/Uno.UI.RuntimeTests/Tests/BindingTests/BindingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Automation;
using Microsoft.UI.Xaml.Controls;
using SamplesApp.UITests;
using Uno.UI.RuntimeTests.Helpers;

namespace Uno.UI.RuntimeTests.Tests;
Expand Down Expand Up @@ -49,4 +50,22 @@ public async Task When_BindingShouldBeAppliedOnPropertyChangedEvent()
Assert.AreEqual(2, converter.ConvertCount);
Assert.AreEqual("1", SUT.myTb.Text);
}

[TestMethod]
[UnoWorkItem("https://github.com/unoplatform/uno/issues/16520")]
public async Task When_XBind_In_Window()
{
var SUT = new XBindInWindow();
SUT.Activate();
try
{
Assert.AreEqual(0, SUT.ClickCount);
SUT.MyButton.AutomationPeerClick();
Assert.AreEqual(1, SUT.ClickCount);
}
finally
{
SUT.Close();
}
}
}
13 changes: 13 additions & 0 deletions src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindInWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Window
x:Class="Uno.UI.RuntimeTests.Tests.XBindInWindow"
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">

<Grid>
<Button Click="{x:Bind Click}" x:Name="MyButton" x:FieldModifier="public" />
</Grid>
</Window>
18 changes: 18 additions & 0 deletions src/Uno.UI.RuntimeTests/Tests/BindingTests/XBindInWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.UI.Xaml;

namespace Uno.UI.RuntimeTests.Tests;

public sealed partial class XBindInWindow : Window
{
public int ClickCount { get; private set; }

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

public void Click(object sender, RoutedEventArgs args)
{
ClickCount++;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ protected void InitializeNativeWindow()

private void OnNativeShown(object? sender, EventArgs e) => ContentManager.TryLoadRootVisual(XamlRoot!);

private void OnNativeClosed(object? sender, EventArgs args) => Closed?.Invoke(this, new WindowEventArgs());
private void OnNativeClosed(object? sender, EventArgs args) => Closed?.Invoke(Window, new WindowEventArgs());

private void OnNativeSizeChanged(object? sender, Size size)
{
Expand All @@ -145,7 +145,7 @@ private void OnNativeSizeChanged(object? sender, Size size)
#if !HAS_UNO_WINUI // CoreWindow has a different WindowSizeChangedEventArgs type, let's skip raising it completely.
CoreWindow?.OnSizeChanged(coreWindowSizeChangedEventArgs);
#endif
SizeChanged?.Invoke(this, windowSizeChanged);
SizeChanged?.Invoke(Window, windowSizeChanged);

XamlRoot?.RaiseChangedEvent();
}
Expand Down Expand Up @@ -174,7 +174,7 @@ private void OnNativeVisibilityChanged(object? sender, bool isVisible)
var args = new VisibilityChangedEventArgs() { Visible = isVisible };

CoreWindow?.OnVisibilityChanged(args);
VisibilityChanged?.Invoke(this, args);
VisibilityChanged?.Invoke(Window, args);
SystemThemeHelper.RefreshSystemTheme();
}

Expand Down Expand Up @@ -211,7 +211,7 @@ private void OnActivationStateChanged(CoreWindowActivationState state)
var coreWindowActivatedEventArgs = activatedEventArgs;
#endif
CoreWindow?.OnActivated(coreWindowActivatedEventArgs);
Activated?.Invoke(this, activatedEventArgs);
Activated?.Invoke(Window, activatedEventArgs);
SystemThemeHelper.RefreshSystemTheme();
}

Expand Down

0 comments on commit 9bd9c2e

Please sign in to comment.