Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(xLoad): fix unload-reload scenarios when using x:Bind with x:Load #19174

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Page x:Class="Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml.Controls.xLoad_Back_And_Forth"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml.Controls"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<NavigationView x:Name="NavView" x:FieldModifier="public" PaneDisplayMode="Left">
<NavigationView.MenuItemTemplate>
<DataTemplate
x:DataType="local:xLoadBackAndForthVM">
<NavigationViewItem x:Name="nvi" IsSelected="{x:Bind IsSelected, Mode=TwoWay}">
<TextBlock
x:Name="tb"
x:Load="{x:Bind IsSelected, Mode=OneWay}"
Text="visible" />
</NavigationViewItem>
</DataTemplate>
</NavigationView.MenuItemTemplate>
</NavigationView>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Microsoft.UI.Xaml.Controls;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml.Controls
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class xLoad_Back_And_Forth : Page
{
public xLoad_Back_And_Forth()
{
this.InitializeComponent();
NavView.MenuItemsSource = new List<xLoadBackAndForthVM> { new(), new() };
}
}

public class xLoadBackAndForthVM : INotifyPropertyChanged
{
private bool _isSelected;
public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

protected bool SetField<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<T>.Default.Equals(field, value)) return false;

Check failure on line 34 in src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Controls/xLoad_Back_And_Forth.xaml.cs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Controls/xLoad_Back_And_Forth.xaml.cs#L34

Add curly braces around the nested statement(s) in this 'if' block.
field = value;
OnPropertyChanged(propertyName);
return true;
}

public bool IsSelected
{
get => _isSelected;
set => SetField(ref _isSelected, value);
}
}
}
23 changes: 23 additions & 0 deletions src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Given_xLoad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,29 @@ public async Task When_xLoad_Visibility_Set()
Assert.AreEqual(1, SUT.GetChildren().Count(c => c is ElementStub));
Assert.AreEqual(0, SUT.GetChildren().Count(c => c is Border));
}

[TestMethod]
[UnoWorkItem("https://github.com/unoplatform/uno/issues/18509")]
public async Task When_xLoad_Set_Back_And_Forth()
{
var SUT = new xLoad_Back_And_Forth();
await UITestHelper.Load(SUT);

Assert.AreEqual("tb", SUT.FindFirstChild<NavigationViewItem>().FindFirstChild<ElementStub>().Name);
Assert.IsNull(SUT.FindFirstChild<NavigationViewItem>().FindFirstChild<TextBlock>());

((List<xLoadBackAndForthVM>)SUT.NavView.MenuItemsSource)[0].IsSelected = true;
await UITestHelper.WaitForIdle();

Assert.AreNotEqual("tb", SUT.FindFirstChild<NavigationViewItem>().FindFirstChild<ElementStub>().Name);
Assert.AreEqual("visible", SUT.FindFirstChild<NavigationViewItem>().FindFirstChild<TextBlock>().Text);

((List<xLoadBackAndForthVM>)SUT.NavView.MenuItemsSource)[0].IsSelected = false;
await UITestHelper.WaitForIdle();

Assert.AreEqual("tb", SUT.FindFirstChild<NavigationViewItem>().FindFirstChild<ElementStub>().Name);
Assert.IsNull(SUT.FindFirstChild<NavigationViewItem>().FindFirstChild<TextBlock>());
}
}
}
#endif
13 changes: 13 additions & 0 deletions src/Uno.UI/UI/Xaml/ElementStub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ private void MaterializeInner()
{
_isMaterializing = true;

// Before swapping out the ElementStub, we set the inherited DC locally so that when removed from the
// visual tree, the inherited DC is kept intact. This is important in case of setting x:Load with an
// x:Bind. The binding we generate from the x:Bind will only listen to property changes while the DC
// is present. This is definitely not what happens on WinUI, but considering our implementation of
// x:Bind and ElementStub differ completely from WinUI's, this is acceptable for now.
// https://github.com/unoplatform/uno/issues/18509
if ((this as IDependencyObjectStoreProvider).Store.GetPropertyDetails(DataContextProperty)
.CurrentHighestValuePrecedence > DependencyPropertyValuePrecedences.Local)
{
this.SetValue(DataContextProperty, DataContext, DependencyPropertyValuePrecedences.Local);
}

Comment on lines +203 to +208
Copy link
Contributor Author

Choose a reason for hiding this comment

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

For the record, I really don't like this, but I don't see a better way of fixing this with the way x:Bind is currently implemented.

_content = SwapViews(oldView: (FrameworkElement)this, newViewProvider: ContentBuilder);
#if ENABLE_LEGACY_TEMPLATED_PARENT_SUPPORT
// note: This can be safely removed, once moving away from legacy impl.
Expand All @@ -220,6 +232,7 @@ private void Dematerialize()

if (_content != null)
{
this.ClearValue(DataContextProperty, DependencyPropertyValuePrecedences.Local);
var newView = SwapViews(oldView: (FrameworkElement)_content, newViewProvider: () => this as View);
if (newView != null)
{
Expand Down
Loading