Wait for the DisplayAlert to be closed

Jassim Al Rahma 1,566 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

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,419 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,853 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 74,641 Reputation points Microsoft Vendor
    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.