UWP - MessageDialog 在 Windows Phone 和平板电脑模式下使应用程序崩溃 [英] UWP - MessageDialog crashes the app on Windows Phone and tablet mode

查看:31
本文介绍了UWP - MessageDialog 在 Windows Phone 和平板电脑模式下使应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Windows 10 通用应用中,我想在按下后退按钮时显示 MessageDialog.

In a Windows 10 Universal app, I want to display a MessageDialog when the back button is pressed.

我的页面代码如下:

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
        SystemNavigationManager.GetForCurrentView().BackRequested += GamePage_BackRequested;
    }

    private async void GamePage_BackRequested(object sender, BackRequestedEventArgs e)
    {
        var dialog = new Windows.UI.Popups.MessageDialog("Are you sure ?");

        dialog.Commands.Add(new Windows.UI.Popups.UICommand("Yes"));
        dialog.Commands.Add(new Windows.UI.Popups.UICommand("No"));

        var result = await dialog.ShowAsync();
    }

当我在本地机器"中启动应用程序时,对话框显示良好.但是,当我将 Windows 切换到平板电脑模式"时,或者当我在 Windows Phone 上尝试时,ShowAsync 方法会导致应用程序崩溃(没有错误).

When I lauch the App in "local machine", the dialog is well displayed. But when I turn Windows to "tablet mode", or when i try it on my Windows Phone, the ShowAsync method crashes the App (with no error).

为什么应用程序崩溃?

推荐答案

问题似乎是应该从 UI 线程调用dialog.ShowAsync()"方法.

The problem seems to be that the "dialog.ShowAsync()" method should be called from the UI thread.

我是这样解决的:

    private void GamePage_BackRequested(object sender, BackRequestedEventArgs e)
    {
        e.Handled = true;
        Frame rootFrame = Window.Current.Content as Frame;            
        if (rootFrame.CanGoBack)
        {
            var d = dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => ShowConfirmationDialog(rootFrame));
        }
    }

    public async void ShowConfirmationDialog(Frame rootFrame)
    {
        var dialog = new Windows.UI.Popups.MessageDialog("Are you sure ?");

        dialog.Commands.Add(new Windows.UI.Popups.UICommand("Yes") { Id = 0 });
        dialog.Commands.Add(new Windows.UI.Popups.UICommand("No") { Id = 1 });

        var result = await dialog.ShowAsync();

        if (result != null && result.Label == "Yes")
        {
            rootFrame.GoBack();
        }
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        dispatcher = Windows.UI.Core.CoreWindow.GetForCurrentThread().Dispatcher;

        SystemNavigationManager.GetForCurrentView().BackRequested += GamePage_BackRequested;
    }

    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        base.OnNavigatedFrom(e);

        SystemNavigationManager.GetForCurrentView().BackRequested -= GamePage_BackRequested;
    }

这篇关于UWP - MessageDialog 在 Windows Phone 和平板电脑模式下使应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆