Skip to content

Commit

Permalink
fix: Correcting handling of datacontext due to inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
nickrandolph authored and jeromelaban committed Oct 27, 2023
1 parent fecdbb9 commit 6609f9b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ private static void SwapViews(FrameworkElement oldView, FrameworkElement newView
parentAsContentControl = parentAsContentControl ?? (VisualTreeHelper.GetParent(oldView) as ContentPresenter)?.FindFirstParent<ContentControl>();
#endif

var parentDataContext = (parentAsContentControl as FrameworkElement)?.DataContext;

if ((parentAsContentControl?.Content as FrameworkElement) == oldView)
{
parentAsContentControl.Content = newView;
Expand Down Expand Up @@ -111,19 +113,19 @@ private static void SwapViews(FrameworkElement oldView, FrameworkElement newView

if (oldView is FrameworkElement oldViewAsFE && newView is FrameworkElement newViewAsFE)
{
PropagateProperties(oldViewAsFE, newViewAsFE);
ApplyDataContext(parentDataContext, oldViewAsFE, newViewAsFE);
}
}

private static void PropagateProperties(FrameworkElement oldView, FrameworkElement newView)
private static void ApplyDataContext(object? parentDataContext, FrameworkElement oldView, FrameworkElement newView)
{
if (oldView == null || newView == null)
{
return;
}

if (newView.DataContext is null
&& oldView.DataContext is not null)
if ((newView.DataContext is null || newView.DataContext == parentDataContext)
&& (oldView.DataContext is not null && oldView.DataContext != parentDataContext))
{
// If the DataContext is not provided by the page itself, it may
// have been provided by an external actor. Copy the value as is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public class Given_Frame_DataContext : BaseTestClass
{
private const string SimpleTextChange = " (changed)";
private const string VMText = " VM Text";

private const string FrameTextBlockOriginalText = "Frame";
private const string FrameVMText = FrameTextBlockOriginalText + VMText;

private const string FirstPageTextBlockOriginalText = "First page";
private const string FirstPageTextBlockChangedText = FirstPageTextBlockOriginalText + SimpleTextChange;
private const string FirstPageVMText = FirstPageTextBlockOriginalText + VMText;
Expand Down Expand Up @@ -157,4 +161,34 @@ await HotReloadHelper.UpdateServerFileAndRevert<HR_Frame_Pages_Page1>(
// Check that after the test has executed, the xaml is back to the original text
await frame.ValidateTextOnChildTextBlock(vm.TitleText, 1);
}


[TestMethod]
public async Task Check_Can_Change_Page1_With_Inherited_DataContext()
{
var ct = new CancellationTokenSource(TimeSpan.FromSeconds(10)).Token;

var frame = new Windows.UI.Xaml.Controls.Frame();
UnitTestsUIContentHelper.Content = frame;

frame.Navigate(typeof(HR_Frame_Pages_Page1));

var frame_vm = new HR_Frame_VM(FrameVMText);
frame.DataContext = frame_vm;
var vm = new HR_Frame_Pages_Page1_VM(FirstPageVMText);
(frame.Content as Page).DataContext = vm;

// Check the initial text of the TextBlock
await frame.ValidateTextOnChildTextBlock(vm.TitleText, 1);

// Check the text of the TextBlock doesn't change
await HotReloadHelper.UpdateServerFileAndRevert<HR_Frame_Pages_Page1>(
FirstPageTextBlockOriginalText,
FirstPageTextBlockChangedText,
() => frame.ValidateTextOnChildTextBlock(vm.TitleText, 1),
ct);

// Check that the text is still the original value
await frame.ValidateTextOnChildTextBlock(vm.TitleText, 1);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
namespace Uno.UI.RuntimeTests.Tests.HotReload.Frame.HRApp.Tests.Pages;

public record HR_Frame_Pages_Page1_VM(string TitleText);

public record HR_Frame_VM(string TitleText);

0 comments on commit 6609f9b

Please sign in to comment.