【发布时间】:2019-06-02 15:46:06
【问题描述】:
为什么下面的代码(感谢Aybe)挂在var result = await dialog1.ShowAsync(); 行我期待它获得简单的用户输入并在用户单击确定后继续。但是对话框 UI 甚至没有完全显示出来。它只是挂在所有白色背景上。我只是看到在主窗口的顶部有一个白色背景的小对话框窗口,它就挂在那里。
我已经看到诸如here 等问题的解决方案。但是正如这个解决方案所暗示的那样,我在 Button_Click 事件上使用了async。引用的解决方案与我的不同之处在于我使用的是UWP,但它们可能不是。那么,我可能缺少什么以及我们如何解决这个问题?
private async void myButton_ClickAsync(object sender, RoutedEventArgs e)
{
var dialog1 = new ContentDialog1();
var result = await dialog1.ShowAsync(); //hangs here
if (result == ContentDialogResult.Primary)
{
var text = dialog1.Text;
}
....
....
}
自定义对话框类:
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace App1
{
public sealed partial class ContentDialog1 : ContentDialog
{
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text", typeof (string), typeof (ContentDialog1), new PropertyMetadata(default(string)));
public ContentDialog1()
{
InitializeComponent();
}
public string Text
{
get { return (string) GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
private void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
}
private void ContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)
{
}
}
}
更新:
我正在使用VS2017 和Windows 10 Professional 笔记本电脑的最新版本。不涉及点击(触摸设备)或智能手机或平板电脑。
【问题讨论】:
-
您的代码确实对我有用。 contentDialog 在我这边运行良好。我无法重现您的问题。您的操作系统构建版本和项目的目标版本是什么?