Skip to content

Commit

Permalink
fix: Prevent FrameView DataContext propagation
Browse files Browse the repository at this point in the history
  • Loading branch information
eriklimakc committed Aug 23, 2024
1 parent 757db1f commit aff57c6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
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;
}

/// <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" />
<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

0 comments on commit aff57c6

Please sign in to comment.