无法在ContentDialog中处理BackButton

时间:2015-12-22 14:13:10

标签: button win-universal-app uwp

在打开ContentDialog并按下设备的硬件/软件BackButton时,是否有办法获取任何事件?

1 个答案:

答案 0 :(得分:1)

在ContentDialog打开时按后退按钮不会引发Windows.UI.Core.SystemNavigationManager.BackRequested事件。

然而,它会关闭ContentDialog,这将按顺序触发ContentDialog的Closing和Closed事件。此外,如果通过后退按钮关闭,ContentDialog.ShowAsync()将返回“无”。以下示例演示了所有三种方法。

var cd = new ContentDialog() {
    Title = "Test Dialog",
    Content = "This is a test content dialog.  Hit the back button now.",
    PrimaryButtonText = "OK",
};
cd.Closing += (ContentDialog s, ContentDialogClosingEventArgs ev) => { new MessageDialog("Event 1 fired.").ShowAsync(); };
cd.Closed += (ContentDialog s, ContentDialogClosedEventArgs ev) => { new MessageDialog("Event 2 fired.").ShowAsync(); };
var result = await cd.ShowAsync();
if (result == ContentDialogResult.None)
{
   new MessageDialog("Back button was pressed.").ShowAsync();
}

希望有所帮助!如果没有,请告诉我。 :)

<强>更新 我想到的另一个解决方案是通过取消它并添加你想要的任何行为来处理结束事件。这会阻止ContentDialog关闭。

private async void Page_Loaded(object sender, RoutedEventArgs e)
{
    var cd = new ContentDialog()
    {
        Title = "Test Dialog",
        Content = "This is a test content dialog.  Hit the back button now.",
        PrimaryButtonText = "OK",
    };
    cd.Closing += Cd_Closing;
    await cd.ShowAsync();
}

private void Cd_Closing(ContentDialog sender, ContentDialogClosingEventArgs args)
{
    if (args.Result == ContentDialogResult.None)
    {
        args.Cancel = true;
        // Handle back press here instead of closing.
    }
}