Mopups
Mopups copied to clipboard
Null Reference Exception when pushing a Mopup in OnAppearing() on Android
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
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());
}