Heathcliff
Heathcliff

Reputation: 21

Win8 store Application, do some check in the OnNavigatedFrom Method

I wanted to do some check, when I click the NavigateButton(Back) to home page. 1.Display the MessageDialog show update the content "yes" or "not" 2.If click yes, update current page. If click No, do nothing 3.navigate to homepage.

I use below code, but currently it will back to the home page first, and then pop up the message dialog. how can I let the action that navigate to homepage happened after I clicked the message dialog

    protected async override void OnNavigatedFrom(NavigationEventArgs e)
    {

        MessageDialog md = new MessageDialog("Do you want to save");
        md.Commands.Add(new UICommand("Yes", (UICommandInvokedHandler) =>
            {
                Application.Current.Exit();
                //this.UpdateClick();
            }));
        md.Commands.Add(new UICommand("No"));
        var task = md.ShowAsync().AsTask();
        await task;
    }

Upvotes: 2

Views: 375

Answers (1)

Jon Rea
Jon Rea

Reputation: 9455

OnNavigateFrom is not cancel-able, and happens after the navigation has already occurred.

Rather than having the code, why don't you re-wire the back button to a click handler which pops up the message box? Then when the user clicks a button, you perform the appropriate navigation.

    private async void OnBackButtonClick(object sender, RoutedEventArgs routedEventArgs)
    {
        MessageDialog md = new MessageDialog("Do you want to save");

        md.Commands.Add(new UICommand("Yes", uiCommandInvokedHandler =>
        {
            // Refresh logic
        }));

        md.Commands.Add(new UICommand("No", uiCommandInvokedHandler =>
        {
            var rootFrame = (Frame)Window.Current.Content;
            rootFrame.Navigate(typeof(MainPage));
        }));

        await md.ShowAsync();
    }

Upvotes: 1

Related Questions