-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
DisplayAlert throws NullReferenceException when called from OnAppearing
#22260
Comments
Hi I'm an AI powered bot that finds similar issues based off the issue title. Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you! Open similar issues:
Closed similar issues:
|
Can you use NavigatedTo or Loaded instead? |
Hi, thanks for the fast reply. Tomorrow I will find the NavigatedTo and I will try it |
Using the OnNavigatedTo doesnt work cause the page hasn yet loaded and the DisplayAlert is still null at that point. Meanwhile as I mentioned I am using the Loaded event from the xaml component but it is a workaround and It is not that great. |
OnAppearing
Hi, Im trying to replicate it in a single project but I cant cause I having other issues :( will try again, but first need to upgrade to .NET 8 maybe something change. Also I do not mentioned that Im using communitytoolkit to generate the page and the other stuff. Dont know if it is relevant |
Description
Hi everybody,
In .NET MAUI, currently, the only lifecycle event available to determine when a page is visible is OnAppearing. However, this event is fired before the UI components are fully loaded and rendered. This timing issue causes problems when performing UI-related operations such as displaying error messages using DisplayAlert immediately in the OnAppearing method. For instance, calling DisplayAlert at this stage results in a NullReferenceException because the components it relies on may not be fully initialized yet.
Steps to Reproduce
Expected Behavior
There should be an event or method, like OnAppeared(), that reliably indicates that the page and all its components have been fully loaded and rendered. This would allow developers to safely interact with the UI elements without risking null reference errors.
Actual Behavior
Currently, the only available event before a page becomes interactable is OnAppearing, which is too early in the lifecycle to safely interact with UI elements, so the app crashes if the NullException is not handled correctly.
Version with bug
8.0.10 SR3
Is this a regression from previous behavior?
Not sure, did not test other versions
Affected platforms
Android, I was not able test on other platforms
Affected platform versions
Android SDK 32
Did you find any workaround?
To address the issue, I implemented a workaround by connecting an event from the XAML component directly to the ViewModel. Here's how I did it:
XAML:
In the XAML, I added an EventToCommandBehavior to the StackLayout that triggers when the Loaded event fires. This behavior binds directly to a command in the ViewModel.
ViewModel:
In the ViewModel, I declared a PageLoadedCommand and defined the corresponding method OnPageLoaded that executes when the command is triggered.
This approach ensures that the PageLoadedCommand is triggered by the Loaded event of the StackLayout, allowing us to handle UI-related tasks right after the components are fully loaded, and thereby avoiding issues like NullReferenceException.
The text was updated successfully, but these errors were encountered: