I have a .NET MAUI project and just want to display an alert. This already worked well, but in some cases it throws me a NullRefereceException. The Code is:
private async void Info_Appearing(object sender, EventArgs e)
{
await Application.Current.MainPage.DisplayAlert("Title", "Text", "OK");
}
I totally have no clue, what the problem is. The stack trace is:
It seems it's a problem of the Android debugger/emulator, because with my local Android device, everything works fine...
Thank you for helping :)
Make sure that you have set the
MainPage
in constructor before it is assigned to theMainPage
property of your App. Otherwise, when using theApp.Current.MainPage
before then, it might throw aNullReferenceException
.As an alternative workaround, you can refer to the snippets below: