maui
maui copied to clipboard
Lack of OnAppeared Lifecycle Event
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
- Override the OnAppearing method in a MAUI ContentPage.
- Invoke DisplayAlert which attempts to use UI components that may not be fully initialized.
- Notice that a NullReferenceException is thrown because the UI elements are accessed before they are fully ready.
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.
<StackLayout.Behaviors>
<toolkit:EventToCommandBehavior
EventName="Loaded"
Command="{Binding PageLoadedCommand}" />
</StackLayout.Behaviors>
ViewModel: In the ViewModel, I declared a PageLoadedCommand and defined the corresponding method OnPageLoaded that executes when the command is triggered.
public ICommand PageLoadedCommand { get; }
public YourViewModel()
{
PageLoadedCommand = new Command(OnPageLoaded);
}
private void OnPageLoaded(object obj)
{
DisplayError("Error", "Page Loaded"); //Custom function that trigger DisplayAlert()
}
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.
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:
- App.Current.PageDisappearing is not raised when a page is popped from the navigation stack. (#14092), similarity score: 0.73
Closed similar issues:
- OnPageAppearing firing before the OnNavigatedTo event for a page (#15256), similarity score: 0.76
- Add Loaded lifecycle event (#8683), similarity score: 0.74
- Alert not shown in OnAppearing method (#19369), similarity score: 0.73
- Error when using DisplayAlert in override void onAppearing (#12739), similarity score: 0.71
Note: You can give me feedback by thumbs upping or thumbs downing this comment.
Can you use NavigatedTo or Loaded instead?
Can you use NavigatedTo or Loaded instead?
Hi, thanks for the fast reply. NavigatedTo and Loaded are both method of the base class? Or the part of the XAML? As I wrote I use Loaded as workaround, but not sure if you meant that loaded.
Tomorrow I will find the NavigatedTo and I will try it Thanks for now
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.
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
It's very hard to determine what might be the cause of this. You mention "this piece of code uses a custom function", yet we can't see the custom code. That makes it impossible to know if this is something that originates in your code or in the .NET MAUI SDK, or the Toolkit is involved too so it might be that.
This is why we ask for a reproduction. Since this is not really actionable as is, and it has been a while since this was reported and worked on, I hope this is resolved by now and I will close this.
If you think there is still something not quite right, please open a new issue providing a reproduction.
Thanks!