为什么 ContentDialog 和 Prism Navigation 会破坏我的 UWP 应用程序?

Why is ContentDialog and Prism Navigation blowing up my UWP app?

我在我的一个 sessionstateaware 页面上显示了一个内容对话框,我很好地关闭了该对话框,并且可以根据需要在同一页面上重新打开另一个对话框。当我离开并返回并尝试重新打开我的对话框时,它因以下错误而爆炸:

WinRT information: Only a single ContentDialog can be open at any time. Additional information: An async operation was not properly started.

我已经尝试了很多方法,但似乎每当我导航离开并返回时,对控件的引用不知何故丢失并创建了另一个?

这里是问题区域的一小段:

if(asyncCommand != null)
asyncCommand.Cancel();

var result = new ContentDialogResult();
if (CanOpenNewDialog)
{

    CanOpenNewDialog = false;
    MyContentDialog.Title = "Homebuyer Options - " + apt.Customer1FullName;
    asyncCommand = MyContentDialog.ShowAsync();

    result = await asyncCommand.AsTask();
}

多次导航时,ContentDialouge 会遇到同样的问题。应用程序只是通过说 "An async operation was not properly started. Only a single ContentDialog can be open at any time."

崩溃

我找到了解决方案。例如,应该等待调用 ContentDialog 的 Show Dialogue 方法。所以将Show Dialogue方法作为一个任务。

    protected async override void OnNavigatedTo(NavigationEventArgs e)
 {
   await ShowDialouge();
 }

 private async Task ShowDialouge()
  {
    ContentDialog dialouge = new ContentDialog();
    await dialouge.ShowAsync();
  }

问题是当我使用 prism 导航时,每次返回我的页面时,我都会重新订阅正在创建对话框的事件。要解决此问题,我只需调用取消订阅 OnNavigatedFrom。