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

Pushing a Popup on startup leads to an NullReferenceException #118

Open
JonasSuchy opened this issue Apr 18, 2024 · 5 comments
Open

Pushing a Popup on startup leads to an NullReferenceException #118

JonasSuchy opened this issue Apr 18, 2024 · 5 comments

Comments

@JonasSuchy
Copy link

Issue:

Pushing a Popup shortly after starting the App, leads to a NullReferenceException, and the Popup does not get pushed.
On Android the App crashes sometimes in this situation , but only in release mode.

Expected Behavior:

The Popup should be opened after the everything necessary was initialized.

Environment:

Platform: Windows, Android, iOS
.NET Version: 8.0.0
Mopups Version: 1.3.1
Maui Version: 8.0.20

Steps to Reproduce:

  1. Clone SampleProject.
  2. Add "MopupService.Instance.PushAsync(new LoginPage());" to MainPage.OnAppearing (Or click on one of the buttons (very!) quickly).
  3. Popup does not open on App startup (and might crash).

Workaround:

Awaiting this method before calling PushAsync fixes the issue:

    private async Task WaitForPopupNavigationInitialization(PopupPage popupPage)
    {
        bool CheckPopupNavigationIsSet()
        {
#if WINDOWS
                return Application.Current?.MainPage?.Handler?.MauiContext?.Services?.GetService<Microsoft.UI.Xaml.Window>()?.Content?.XamlRoot is not null;
#elif ANDROID
                return (MauiApplication.Current?.Application?.Windows?.FirstOrDefault()?.Content as Element)?.Handler?.MauiContext is not null;
#elif IOS
                return popupPage?.Handler is not null || Application.Current?.MainPage?.Handler?.MauiContext is not null;
#else
                return false;
#endif
        }
        while (!CheckPopupNavigationIsSet())
        {
            await Task.Delay(100);
        }
    }

Probably not the cleanest way to prevent the issue, but maybe something similar could be integrated into Mopups.

@sheharyarshahid
Copy link

I've had this issue after updating to MAUI 8.0.20 on iOS only. Android and Windows seems to work fine as of now.

@JonasSuchy
Copy link
Author

The is issue is reproducible on all three platforms for me.

@sheharyarshahid
Copy link

Do you know any temporary workaround? I tried the above one but didn't work. Still NullReferenceException is throwing

@JonasSuchy
Copy link
Author

In your own project, or is the problem reproducible in the sample project?
Does the stack trace say where the exception occurs?

@sheharyarshahid
Copy link

Okay, so It had something to do with using ListView ItemSource binding inside Popup. Removing item source binding from XAML and assigning it in code behind resolves the issue for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants