c# - UWP 等待用户与 ContentDialog 交互

标签 c# asynchronous dialog uwp async-await

在我的 UWP 应用程序中,我显示了一个带有少量 TextBoxContentDialog 并根据用户输入执行一些操作。我想要做的是这样的:

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

在用户单击对话框的主要或次要按钮之前继续执行。我只想在用户与对话框交互后继续。

最佳答案

方法一

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

方法二

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

Asynchronous programming 了解异步等待和 Call asynchronous APIs in C#文档

关于c# - UWP 等待用户与 ContentDialog 交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44574052/

相关文章:

c# - 如何从代码中隐藏一个 div (c#)

c# - ASP.NET 4.5.2 MVC 项目模板中的身份验证系统是否适合在实际应用程序中使用?

Android 从对话框启动浏览器

java - Android:在布局中出现 "Level Complete"对话框的方法

c++ - MFC:如何创建带有透明 PNG 作为背景(而不是窗口镶边)的 Skinnied Dialog?

c# - 从 .NET Core 项目引用可移植类库

c# - 术语 "true"面向对象是什么意思

Node.js promise 、异步或只是回调

javascript - Nodejs如何使用多个await promise

javascript - 在 useEffect 中响应异步数据获取