Skip to content
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

Open
Trikos opened this issue May 7, 2024 · 5 comments
Open
Labels
area-controls-dialogalert DisplayAlert, dialog area-core-lifecycle XPlat and Native UIApplicationDelegate/Activity/Window lifecycle events platform/android 🤖 s/needs-attention Issue has more information and needs another look t/bug Something isn't working
Milestone

Comments

@Trikos
Copy link

Trikos commented May 7, 2024

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

  1. Override the OnAppearing method in a MAUI ContentPage.
  2. Invoke DisplayAlert which attempts to use UI components that may not be fully initialized.
  3. 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.

@Trikos Trikos added the t/bug Something isn't working label May 7, 2024
Copy link
Contributor

github-actions bot commented May 7, 2024

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:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

@PureWeen
Copy link
Member

PureWeen commented May 7, 2024

Can you use NavigatedTo or Loaded instead?

@PureWeen PureWeen added s/needs-info Issue needs more info from the author area-core-lifecycle XPlat and Native UIApplicationDelegate/Activity/Window lifecycle events area-controls-pages Page types labels May 7, 2024
@dotnet-policy-service dotnet-policy-service bot added the legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label May 7, 2024
@Trikos
Copy link
Author

Trikos commented May 8, 2024

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

@dotnet-policy-service dotnet-policy-service bot added s/needs-attention Issue has more information and needs another look and removed s/needs-info Issue needs more info from the author labels May 8, 2024
@Trikos
Copy link
Author

Trikos commented May 8, 2024

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.

@PureWeen PureWeen added s/needs-repro Attach a solution or code which reproduces the issue and removed s/needs-attention Issue has more information and needs another look labels May 8, 2024
@PureWeen PureWeen changed the title Lack of OnAppeared Lifecycle Event DisplayAlert throws NullReferenceException when called from OnAppearing May 8, 2024
@Eilon Eilon removed the legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label May 10, 2024
@PureWeen PureWeen added area-controls-dialogalert DisplayAlert, dialog and removed area-controls-pages Page types labels May 10, 2024
@Trikos
Copy link
Author

Trikos commented May 14, 2024

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

@dotnet-policy-service dotnet-policy-service bot added s/needs-attention Issue has more information and needs another look and removed s/needs-repro Attach a solution or code which reproduces the issue labels May 14, 2024
@jsuarezruiz jsuarezruiz added this to the Backlog milestone Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-dialogalert DisplayAlert, dialog area-core-lifecycle XPlat and Native UIApplicationDelegate/Activity/Window lifecycle events platform/android 🤖 s/needs-attention Issue has more information and needs another look t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants