ravi kumar
ravi kumar

Reputation: 1620

UWP Waiting for user interaction with ContentDialog

In my UWP application I am showing a ContentDialog with few TextBox and based on the user input perform some action. What I want to do is something like this:

ContentDialogResult result = await LoginDialog.ShowAsync();
//Nothing else should be executed before the below method finishes executing
//other code
//....
//....
private void DialogPrimaryButton_ClickAsync(object sender, RoutedEventArgs e)
{
    SomethingSynchronous();
}

I am a newbie not able to understand async-await properly and what's happening is that the code that follows the line

ContentDialogResult result = await LoginDialog.ShowAsync();

continues to execute before the user clicks on the dialog's primary or secondary button. I want to move on only after user interacts with the dialog.

Upvotes: 4

Views: 2213

Answers (1)

Vijay Nirmal
Vijay Nirmal

Reputation: 5837

Method 1

private async void DialogPrimaryButton_ClickAsync(object sender, RoutedEventArgs e)
{
    await DisplayContentDialog();
}

private async Task DisplayContentDialog()
{
    ContentDialogResult result = await LoginDialog.ShowAsync();

    //For Primary, Secondary and Cancel Buttons inside the ContentDialog
    if (result == ContentDialogResult.Primary)
    {
        OutputText.Text = "Primary";
        // User Pressed Primary key
    }
    else if (result == ContentDialogResult.Secondary)
    {
        OutputText.Text = "Secondary";
        // User Pressed Secondary key
    }
    else
    {
        OutputText.Text = "Cancel";
        // User pressed Cancel, ESC, or the back arrow.
    }
}

//For custom Buttons inside the ContentDialog
//Use Button Click event for the custom Buttons inside the ContentDialog
private void XAMLButton_Click(object sender, RoutedEventArgs e)
{
    OutputText.Text = "XAML Button";
    LoginDialog.Hide();
}

Method 2

private async void DialogPrimaryButton_ClickAsync(object sender, RoutedEventArgs e)
{
    await DisplayContentDialog();
}

private async Task DisplayContentDialog()
{
    XAMLButton.Click += XAMLButton_Click;
    LoginDialog.PrimaryButtonClick += LoginDialog_PrimaryButtonClick;
    LoginDialog.SecondaryButtonClick += LoginDialog_SecondaryButtonClick;
    LoginDialog.CloseButtonClick += LoginDialog_CloseButtonClick;
    await LoginDialog.ShowAsync();
}

//For Primary Button inside the ContentDialog
private void LoginDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
    OutputText.Text = "Primary";
}

//For Secondary Button inside the ContentDialog
private void LoginDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
    OutputText.Text = "Secondary";
}

//For Cancel Buttons inside the ContentDialog
private void LoginDialog_CloseButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
    OutputText.Text = "Cancel";
}

//For custom Buttons inside the ContentDialog
private void XAMLButton_Click(object sender, RoutedEventArgs e)
{
    OutputText.Text = "XAML Button";
    LoginDialog.Hide();
}

Learn about async-await from Asynchronous programming and Call asynchronous APIs in C# document

Upvotes: 3

Related Questions