简体   繁体   English

在前进代码之前,无法通过MessageDialog从用户检索输入

[英]Unable to retrieve input from user via MessageDialog before the code moving forward

I am trying to invoke a MessageDialog that will prompt for user input, and then proceed with that input. 我试图调用一个MessageDialog,提示您输入用户,然后继续进行该输入。

Below is the code for that. 下面是该代码。

XAML XAML

<Button Content="Click" Click="Button_Click"/>

XAML.CS XAML文件

public MainPage()
{
    this.InitializeComponent();
}
private int _res;
private void Button_Click(object sender, RoutedEventArgs e)
{
    Click_Helper(sender, e);
    System.Diagnostics.Debug.WriteLine(_res.ToString());
    //Some other work will be done here
}

private async Task Click_Helper(object sender, RoutedEventArgs e)
{
    MessageDialog msgbox = new MessageDialog("Hello there");

    msgbox.Commands.Clear();
    msgbox.Commands.Add(new UICommand { Label = "Yes", Id = 0 });
    msgbox.Commands.Add(new UICommand { Label = "No", Id = 1 });

    var result = await msgbox.ShowAsync();
    _res = (int)result.Id;
}

In the above code, I want the _res to be set Before the Debug Writeline, but somehow I can't achieve that. 在上面的代码中,我希望在Debug Writeline之前设置_res,但是以某种方式我无法实现。 Unfortunately for UWP, it seems the MessageDialog doesn't have any option for me to use it synchronously. 不幸的是,对于UWP,似乎MessageDialog没有可供我同步使用的任何选项。

I have been trying to use .Wait() method for the Task but it causes deadlock (I read somewhere about this and it had something to do with UI thread being locking itself from showing the MessageDialog) I also tried several other methods but none works. 我一直在尝试对Task使用.Wait()方法,但它会导致死锁(我读到了有关此内容的信息,这与UI线程被锁定而无法显示MessageDialog有关)我也尝试了其他几种方法,但均无效果。 I am starting to think this is not possible. 我开始认为这是不可能的。

Please help shed some lights on this. 请帮助对此有所帮助。

In the above code, I want the _res to be set Before the Debug Writeline, 在上面的代码中,我希望在Debug Writeline之前设置_res,

Please add the await keyword before Click_Helper method. 请在Click_Helper方法之前添加await关键字。

private async void Button_Click(object sender, RoutedEventArgs e)
{
    await Click_Helper(sender, e);
    System.Diagnostics.Debug.WriteLine(_res.ToString());

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM