簡體   English   中英

UWP等待用戶與ContentDialog的交互

[英]UWP Waiting for user interaction with ContentDialog

在我的UWP應用程序中,我顯示的ContentDialog包含少量TextBox並根據用戶輸入執行一些操作。 我想做的是這樣的:

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();
}

我是一個新手,不能正確理解async-await,發生的事情就是跟隨該行的代碼

ContentDialogResult result = await LoginDialog.ShowAsync();

在用戶單擊對話框的主要或輔助按鈕之前繼續執行。 我想在用戶與對話框交互后繼續前進。

方法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();
}

方法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();
}

了解async-await來自異步編程C#文檔中的調用異步API

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM