Wait for the DisplayAlert to be closed

Jassim Al Rahma 1,616 Reputation points
2023-03-28T23:24:02.3366667+00:00

Hi,

I have below code:

await App.Current.MainPage.DisplayAlert("Congratulations", "Your account is now active.", "Sign in");
App.Current.MainPage = new NavigationPage(new Signin());

The problem is that the code is immediately navigating to the Signin page although I have awaited the DisplayAltert.

I want the code to wait so the user will read the message and click on the DisplayAlert button.

Kidly help.

Thanks,

Jassim

Developer technologies | .NET | .NET MAUI
Developer technologies | C#
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2023-03-29T02:46:31.91+00:00

    Hello,

    I want the code to wait so the user will read the message and click on the DisplayAlert button.

    You can do this by Task.ContinueWith Method. And change the mainpage to Signin page, you need to run it on the Main thread, please refer to following code.

    await App.Current.MainPage.DisplayAlert("Congratulations", "Your account is now active.", "Sign in").ContinueWith( (continueAction) =>
          {
             if (continueAction.IsCompleted)
              {
                  MainThread.BeginInvokeOnMainThread(() =>
                  {
                      // Code to run on the main thread
                      App.Current.MainPage = new NavigationPage(new Signin());
                  });
              }      
          });
    
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.