From c114627eafa57605fbf1cd0897577354413984c0 Mon Sep 17 00:00:00 2001 From: Dan Siegel Date: Fri, 31 May 2024 16:08:11 -0600 Subject: [PATCH] fix: delay nulling BindingContext on Android --- src/Maui/Prism.Maui/Common/MvvmHelpers.cs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Maui/Prism.Maui/Common/MvvmHelpers.cs b/src/Maui/Prism.Maui/Common/MvvmHelpers.cs index 0754c73b9..f6bc06130 100644 --- a/src/Maui/Prism.Maui/Common/MvvmHelpers.cs +++ b/src/Maui/Prism.Maui/Common/MvvmHelpers.cs @@ -63,10 +63,20 @@ public static void DestroyPage(IView? view) InvokeViewAndViewModelAction(view, v => v.Destroy()); - if (view is Page page) + if (view is VisualElement visualElement) { - page.Behaviors?.Clear(); - page.BindingContext = null; +#if ANDROID + visualElement.Unloaded += VisualElementUnloaded; + void VisualElementUnloaded(object? sender, EventArgs e) + { + visualElement.Unloaded -= VisualElementUnloaded; + visualElement.Behaviors?.Clear(); + visualElement.BindingContext = null; + } +#else + visualElement.Behaviors?.Clear(); + visualElement.BindingContext = null; +#endif } } catch (Exception ex) @@ -75,7 +85,7 @@ public static void DestroyPage(IView? view) } } - private static void DestroyChildren(IView page) + private static void DestroyChildren(IView? page) { switch (page) {