2017-06-15 71 views
4

在我的UWP应用程序中,我展示了一个ContentDialog和几个TextBox,并基于用户输入执行某些操作。我想要做的是这样的:UWP正在等待用户与ContentDialog的交互

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

我无法理解异步等待正确和发生的事情是一个新手,那后面的线

ContentDialogResult result = await LoginDialog.ShowAsync(); 

继续执行代码在用户点击对话框的主要或次要按钮之前。我只想在用户与对话框交互之后继续前进。从Asynchronous programmingCall asynchronous APIs in C#文件

回答

2

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

了解异步等待

+0

能否请你告诉我如何做同样的事情如果这些按钮是ContentDialog中的定制XAML按钮(而不是主o r次要) –

+0

@ravikumar使用按钮单击事件来做到这一点 –

+0

@ravikumar我已经更新了我的答案 –