LuckyDucko / Mopups

Popups For MAUI
BSD 3-Clause "New" or "Revised" License
259 stars 49 forks source link

Null Reference Exception when pushing a Mopup in OnAppearing() on Android #110

Open melvyniandrag opened 6 months ago

melvyniandrag commented 6 months ago

Title + code below explain the issue.

Porting a Xamarin app to MAUI. We had an RG popup that would get shown inside OnAppearing() of the main page of the app. After porting to MAUI, this gives a null ref exception, unless I put the mopup PushAsync() inside Device.BeginInvokeOnMainThread.

Here is a demo application on github that illustrates the bug. That link has the relevant code excerpt + a pic of the error message in the readme.

VS 2022 v17.9.0 Mopups 1.3.0

melvyniandrag commented 5 months ago

Update another way around this is to add a delay before the PushAsync. 1 second was sufficient on iOS, but on windows I've had to add a 3 second delay.

        private const int DELAY_FOR_MOPUPS_TO_NOT_CRASH_WITH_NULL_REF_EXCEPTION = 3000;

        protected override async void OnAppearing()
        {
            base.OnAppearing();
            Debug.WriteLine("MainPage - OnAppearing()");
            Microsoft.Maui.Devices.DeviceDisplay.KeepScreenOn = true;
            showOnAppearingPopups();

        }

        private async void showOnAppearingPopups()
        {
            await Task.Delay(DELAY_FOR_MOPUPS_TO_NOT_CRASH_WITH_NULL_REF_EXCEPTION);
            await MopupService.Instance.PushAsync(new MyCustomPopupPage());
        }