1

我需要MessageDialog在启动屏幕期间等待我的 UWP 应用程序启动时的结果。所以,我把它MessageDialog放在MainPage构造函数中:

private async Task ParseConfiguration()
{
    var dialog = new MessageDialog("Message", "Title");
    dialog.Commands.Add(new UICommand { Label = "Exit", Id = 0 });

    await dialog.ShowAsync();
}


public MainPage()
{
    ParseConfiguration();   // works, but I need to wait
    ParseConfiguration().Wait(); // never exits this call
}

我该如何解决这个问题?

4

1 回答 1

1

您通过等待该任务来阻塞您的 UI 线程,因此无法显示对话框(显然也需要显示 UI 线程)并且整个事情都死锁了。但是,无论如何,页面构造函数都不是这样做的好地方(以及一般的构造函数)。相反(例如,这不是唯一的地方)你可以这样做Application.OnLaunched

protected override async void OnLaunched(LaunchActivatedEventArgs e) {
    // some other code here
    // parse configuration before main window is shown
    await ParseConfiguration();
    // some more code here, including showing main windo
}

这将在初始屏幕期间显示您的对话框,但在显示主页之前(如您所愿)。如果出现问题,您也可以在此时终止整个应用程序。

于 2016-06-07T12:40:42.493 回答