Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Windows] Fix crash when navigating pages (#25740)
### Description of Change Fixes a long-standing crash when navigating pages: 1. Manually clear the `ContentPresenter.Content` property. When navigating pages it was discovered that the `Content` property of the `ContentPresenter` sometimes was not being detached from the parent `ContentPresenter` (the `ContentPresenter` itself does detach from the Page). A workaround for this was to manually clear the `Content` property. **The actual cause of this bug:** I suspect that something causing the lifecycle tracking to be messed up. I'm still not sure what this is, but for now this will help our customers more than nothing. ## Workaround The following can be used as a workaround. Place it in the `Loaded` event for your `AppShell`: ```csharp private void AppShell_Loaded(object? sender, EventArgs e) { Loaded -= AppShell_Loaded; #if WINDOWS if (Handler != null && Handler.PlatformView is ShellView shellView && shellView.Content is MauiNavigationView nv && nv.Content is Microsoft.UI.Xaml.Controls.Frame frame) { frame.Navigating += (s, e) => { if (frame.Content is Microsoft.UI.Xaml.Controls.Page page) { page.Unloaded += PageUnloaded; void PageUnloaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e) { page.Unloaded -= PageUnloaded; if (page.Content is Microsoft.UI.Xaml.Controls.ContentPresenter presenter) { presenter.Content = null; } }; } }; } #endif } ``` ### Issues Fixed Fixes #22790 #18441 #22131
- Loading branch information