如何从 Windows Phone 8.1 中的代码中关闭 MessageDialog

How to dismiss a MessageDialog from code in Windows Phone 8.1

如何在 Windows Phone 8.1 中以编程方式关闭消息对话框。我使用 showAsync() 创建了对话框。如果这不可能,那么创建具有以下属性的自定义消息对话框的最佳方法是:

1. It can show test and hold buttons for user interaction.
2. It can be dismissed programmatically
3. Should block the view as a Normal MessageDialog do

使用 ContentDialog 而不是 MessageDialog。这也将允许自定义对话框,这样你就不需要编写自定义控件,除非你想做一些非常疯狂的事情。

在 Windows 上 MessageDialog 是可取消的,但在 Windows 上不能取消 Phone:

// Cancel the MessageDialog after 3 seconds on Windows
private async void Button_Click(object sender, RoutedEventArgs e)
{
    MessageDialog md = new MessageDialog("Lorem ipsum dolor sit amet","Message Dialog Title");
    var t = md.ShowAsync();

    await Task.Delay(TimeSpan.FromSeconds(3));

    // Ignored by the Windows Phone MessageDialog
    t.Cancel();
}

您可以使用 Windows Phone 上的类似代码取消 ContentDialog。如果需要,您可以使用 Visual Studio 的 ContentDialog 模板创建自定义 ContentDialog。

private async void Button_Click(object sender, RoutedEventArgs e)
{
    ContentDialog cd = new ContentDialog();
    cd.Title = "Content Dialog";
    cd.PrimaryButtonText = "Close";
    cd.Content = "Lorem ipsum dolor sit amet";
    var t = cd.ShowAsync();

    await Task.Delay(TimeSpan.FromSeconds(3));

    t.Cancel();
}