From 47dbddc3a0bbe43198832f6e96643ce529730be1 Mon Sep 17 00:00:00 2001 From: Nick Randolph Date: Thu, 20 Jun 2024 15:34:59 +1000 Subject: [PATCH 1/2] fix: Remove ApplyDataContext method (deprecated) that's causing invalid data context --- .../ClientHotReloadProcessor.Common.cs | 25 ------------------- 1 file changed, 25 deletions(-) diff --git a/src/Uno.UI.RemoteControl/HotReload/ClientHotReloadProcessor.Common.cs b/src/Uno.UI.RemoteControl/HotReload/ClientHotReloadProcessor.Common.cs index 10483fc1c830..7f060c3b0a0f 100644 --- a/src/Uno.UI.RemoteControl/HotReload/ClientHotReloadProcessor.Common.cs +++ b/src/Uno.UI.RemoteControl/HotReload/ClientHotReloadProcessor.Common.cs @@ -76,8 +76,6 @@ private static void SwapViews(FrameworkElement oldView, FrameworkElement newView parentAsContentControl = parentAsContentControl ?? (VisualTreeHelper.GetParent(oldView) as ContentPresenter)?.FindFirstParent(); #endif - var parentDataContext = (parentAsContentControl as FrameworkElement)?.DataContext; - if ((parentAsContentControl?.Content as FrameworkElement) == oldView) { parentAsContentControl.Content = newView; @@ -110,29 +108,6 @@ private static void SwapViews(FrameworkElement oldView, FrameworkElement newView VisualTreeHelper.SwapViews(oldView, newView); } #endif - - if (oldView is FrameworkElement oldViewAsFE && newView is FrameworkElement newViewAsFE) - { - ApplyDataContext(parentDataContext, oldViewAsFE, newViewAsFE); - } - } - - private static void ApplyDataContext(object? parentDataContext, FrameworkElement oldView, FrameworkElement newView) - { - if (oldView == null || newView == null) - { - return; - } - - 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 - // in the DataContext of the new element. - - newView.DataContext = oldView.DataContext; - } } } } From b9abdbb8cbf3eaef5070876b7fd31b0f76c7e194 Mon Sep 17 00:00:00 2001 From: Nick Randolph Date: Thu, 20 Jun 2024 15:47:23 +1000 Subject: [PATCH 2/2] chore: Adding back ApplyDataContext for targets where HAS_UNO is false --- .../ClientHotReloadProcessor.Common.cs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/Uno.UI.RemoteControl/HotReload/ClientHotReloadProcessor.Common.cs b/src/Uno.UI.RemoteControl/HotReload/ClientHotReloadProcessor.Common.cs index 7f060c3b0a0f..f76c9478e67e 100644 --- a/src/Uno.UI.RemoteControl/HotReload/ClientHotReloadProcessor.Common.cs +++ b/src/Uno.UI.RemoteControl/HotReload/ClientHotReloadProcessor.Common.cs @@ -76,6 +76,10 @@ private static void SwapViews(FrameworkElement oldView, FrameworkElement newView parentAsContentControl = parentAsContentControl ?? (VisualTreeHelper.GetParent(oldView) as ContentPresenter)?.FindFirstParent(); #endif +#if !HAS_UNO + var parentDataContext = (parentAsContentControl as FrameworkElement)?.DataContext; + var oldDataContext = oldView.DataContext; +#endif if ((parentAsContentControl?.Content as FrameworkElement) == oldView) { parentAsContentControl.Content = newView; @@ -108,6 +112,37 @@ private static void SwapViews(FrameworkElement oldView, FrameworkElement newView VisualTreeHelper.SwapViews(oldView, newView); } #endif + +#if !HAS_UNO + if (oldView is FrameworkElement oldViewAsFE && newView is FrameworkElement newViewAsFE) + { + ApplyDataContext(parentDataContext, oldViewAsFE, newViewAsFE, oldDataContext); + } +#endif } + +#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) + && (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 = oldDataContext; + } + } +#endif } }