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: Prevent FrameView DataContext propagation #2451

Merged
merged 1 commit into from
Aug 23, 2024
Merged
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
3 changes: 3 additions & 0 deletions src/Uno.Extensions.Navigation.UI/Controls/FrameView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ public sealed partial class FrameView : UserControl
public FrameView()
{
InitializeComponent();

// Prevent inheritance of DataContext from Parent to avoid propagation to Children
DataContext = null;
eriklimakc marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,19 @@ public void When_PageNavigationRegisteredRoot()

}

[Test]
public void When_PageNavigationDataContextDidntChange()
{
InitTestSection(TestSections.Navigation_PageNavigation);

App.WaitThenTap("ShowOnePageButton");
App.WaitThenTap("OnePageGetUrlFromBrowser");

// If DataContext is ever changed to anything other than the expected
// the text will be "DataContext is not correct"
App.WaitForText("OnePageTxtDataContext", "DataContext is ok");
}

private void OnePageToFivePage()
{
App.WaitThenTap("OnePageToTwoPageButton");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
Click="{x:Bind ViewModel.SettingsWriteTest}"
Content="Settings Write Test" />
<TextBlock x:Name="TxtUrl" AutomationProperties.AutomationId="OnePageTxtUrlFromBrowser" />
<TextBlock x:Name="TxtDataContext"
Text="DataContext is ok"
AutomationProperties.AutomationId="OnePageTxtDataContext" />
eriklimakc marked this conversation as resolved.
Show resolved Hide resolved
<Button AutomationProperties.AutomationId="OnePageGetUrlFromBrowser"
Click="GetUrlFromBrowser"
Content="Get URL" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,32 @@ namespace TestHarness.Ext.Navigation.PageNavigation;
public sealed partial class PageNavigationOnePage : Page
{
public PageNavigationOneViewModel? ViewModel => DataContext as PageNavigationOneViewModel;

private Type expectedDataContext = typeof(PageNavigationOneViewModel);

public PageNavigationOnePage()
{
this.InitializeComponent();

this.DataContextChanged += PageNavigationOnePage_DataContextChanged;
}

private void PageNavigationOnePage_DataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
{
// For the test `When_PageNavigationDataContextDidntChange`
// If DataContext is ever changed to anything other than the expected the text will be "DataContext is not correct"
// So that we can validade on the test
if (sender.DataContext is { } dataContext && dataContext.GetType() != expectedDataContext)
{
TxtDataContext.Text = "DataContext is not correct";
}
}

public async void OnePageToTwoPageCodebehindClick(object sender, RoutedEventArgs e)
{
await this.Navigator()!.NavigateViewAsync<PageNavigationTwoPage>(this);
}


public async void GetUrlFromBrowser(object sender, RoutedEventArgs e)
{
#if __WASM__
Expand Down
Loading