(Go: >> BACK << -|- >> HOME <<)

Skip to content

Commit

Permalink
Merge pull request #17239 from unoplatform/dev/nr/fixandroidhr
Browse files Browse the repository at this point in the history
fix: Remove ApplyDataContext method (deprecated) that's causing invalid data context
  • Loading branch information
jeromelaban committed Jun 28, 2024
2 parents f2c7afe + b9abdbb commit 7dca68c
Showing 1 changed file with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,10 @@ private static void SwapViews(FrameworkElement oldView, FrameworkElement newView
parentAsContentControl = parentAsContentControl ?? (VisualTreeHelper.GetParent(oldView) as ContentPresenter)?.FindFirstParent<ContentControl>();
#endif

#if !HAS_UNO
var parentDataContext = (parentAsContentControl as FrameworkElement)?.DataContext;

var oldDataContext = oldView.DataContext;
#endif
if ((parentAsContentControl?.Content as FrameworkElement) == oldView)
{
parentAsContentControl.Content = newView;
Expand Down Expand Up @@ -124,28 +126,36 @@ private static void SwapViews(FrameworkElement oldView, FrameworkElement newView
}
#endif

#if !HAS_UNO
if (oldView is FrameworkElement oldViewAsFE && newView is FrameworkElement newViewAsFE)
{
ApplyDataContext(parentDataContext, oldViewAsFE, newViewAsFE);
ApplyDataContext(parentDataContext, oldViewAsFE, newViewAsFE, oldDataContext);
}
#endif
}

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

if ((newView.DataContext is null || newView.DataContext == parentDataContext)
&& (oldView.DataContext is not null && oldView.DataContext != parentDataContext))
&& (oldDataContext is not null && oldDataContext != 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
// in the DataContext of the new element.

newView.DataContext = oldView.DataContext;
newView.DataContext = oldDataContext;
}
}
#endif
}
}

0 comments on commit 7dca68c

Please sign in to comment.