Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Windows] Fix crash when navigating pages #25740

Merged
merged 7 commits into from
Nov 19, 2024
Merged

[Windows] Fix crash when navigating pages #25740

merged 7 commits into from
Nov 19, 2024

Conversation

Foda
Copy link
Member

@Foda Foda commented Nov 7, 2024

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:

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

@Foda Foda requested a review from PureWeen November 7, 2024 22:30
@Foda Foda requested a review from a team as a code owner November 7, 2024 22:30
@Foda Foda requested a review from rmarinho November 7, 2024 22:30
{
HorizontalAlignment = UI.Xaml.HorizontalAlignment.Stretch,
VerticalAlignment = UI.Xaml.VerticalAlignment.Stretch
};

// It's possible that the unloaded event didn't happen yet, so force clear the content
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: we don't just run this by default because when the transition is animated the page flashes blank.

Copy link
Member

@rmarinho rmarinho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also clear/set to null _previousContent on Disconnect ?

@PureWeen PureWeen added this to the .NET 9 SR1.1 milestone Nov 18, 2024
@BaY1251
Copy link
Contributor

BaY1251 commented Nov 19, 2024

There are similar issues on the Android platform #18331.

@Foda Foda merged commit fa519a2 into main Nov 19, 2024
108 checks passed
@Foda Foda deleted the foda/NavCrash branch November 19, 2024 17:35
@PureWeen
Copy link
Member

/backport to release/9.0.1xx

Copy link
Contributor

Started backporting to release/9.0.1xx: https://github.com/dotnet/maui/actions/runs/11920307270

@AwsomeCode
Copy link

Hi @Foda
Can you please check this #26214

@samhouts samhouts added fixed-in-9.0.21 fixed-in-net8.0-nightly This may be available in a nightly release! labels Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-navigation NavigationPage fixed-in-9.0.21 fixed-in-net8.0-nightly This may be available in a nightly release! platform/windows 🪟
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

WinRt exception when page navigated
7 participants