【问题标题】:Why call to ContentDialog.ShowAsync() hangs为什么调用 ContentDialog.ShowAsync() 挂起
【发布时间】: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)
        {
        }
    }
}

更新

我正在使用VS2017Windows 10 Professional 笔记本电脑的最新版本。不涉及点击(触摸设备)或智能手机或平板电脑。

【问题讨论】:

  • 您的代码确实对我有用。 contentDialog 在我这边运行良好。我无法重现您的问题。您的操作系统构建版本和项目的目标版本是什么?

标签: c# uwp


【解决方案1】:

完全按照您的操作,我能够成功地显示对话框。

有一些我们在提供的代码中没有看到的东西,或者是因为它工作得很好。

【讨论】:

    猜你喜欢
    • 2018-03-12
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    • 2018-07-29
    • 1970-01-01
    • 2011-08-13
    • 2020-11-02
    • 1970-01-01
    相关资源
    最近更新 更多