UWP - MessageDialog disables the application in Windows Phone and tablet mode

In a Windows 10 Universal application, I want to display a MessageDialog when I click the back button.

My page code is as follows:

    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();
    }

When I use the application in the "local machine", the dialog is well displayed. But when I turn Windows into “tablet mode”, or when I try to use it on my Windows phone, the ShowAsync method causes the application to crash (no errors).

Why is the application crashing?

+4
source share
3 answers

The problem is that the "dialog.ShowAsync ()" method must be called from the user interface thread.

Here's how I solved it:

    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;
    }
+2

backrequest; e.handled = true;

private async void GamePage_BackRequested(object sender, BackRequestedEventArgs e)
        {
            e.handled = true;
            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();
    }

onnavigatedfrom , , !

 protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            if (gb.DetectPlatform() == Platform.WindowsPhone)
                HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
           elde
                SystemNavigationManager.GetForCurrentView().BackRequested -= GamePage_BackRequested;
};
        }
+1

, - back key ( ), .

, , , Handled, , , "", . , , , (. )

, , Windows 10 , ( "" ). ( ).

0
source

Source: https://habr.com/ru/post/1608885/


All Articles