简体   繁体   中英

Skip a page when the back button is pressed, WP7

In the current released version of WP7 you have no programmatic control over the page stack.

I start at A, go to B, and then C. B is the data entry page for new items, so coming back from CI want to logically land at A. Currently I listen for navigation on B that comes from C and force another back onto A. However, the event doesn't happen soon enough to stop the page from displaying on-screen.

A -> B -> C

C -> A

A is the top level list page. B is the new item page. C is the item detail page.

This only occurs on one form so far so my workaround is to override OnNavigatedTo in the page I want to skip, and call "go back" programmatically. However, this has the undesired effect of briefly showing the page and then immediately navigating off of it.

  • Is there a workable way to stop the flicker?
  • Should my workaround instead be to take full control of page navigation, including go backs? This will leave the page stack is a strange state, but that would be hidden from the user if I control all navigation.

I know there is a new feature in Mango to pop a page from the page stack programmatically, but I'm curious to know if there is a solution to the problem in the released version.

Another motivation for this is I don't know what version will be best to target, the latest, or the one that is just enough for the app. For the time being I'm sticking with the live version.

你应该阅读这篇关于解决循环导航问题的博客文章 ,你可以下载这个演示非线性导航的食谱代码

I have stopped the flickering by making the root frame transparent for the duration of the skip. This example isn't straight from my code.

Firstly on the page you wish to skip, override OnNavigatedTo and test to see where you have come from (this is where my code gets specific, I keep track of where I am):

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    // If I've come from page C, go back again.
    NavigationService.GoBack();
}

Secondly, in the main App.xaml.cs register an event handler for Navigating (I put it in public App() constructor):

RootFrame.Navigating += RootFrame_Navigating;

Finally, flesh them out to hide the frame and show it again for the duration of the skip:

    private bool _skipped;

    private void RootFrame_Navigated(object sender, NavigationEventArgs e)
    {
        RootFrame.Opacity = 100;
        RootFrame.Navigated -= RootFrame_Navigated;
    }

    private void RootFrame_Navigating(object sender, NavigatingCancelEventArgs e)
    {
        if (_skipped)
        {
            _skipped = false;
            RootFrame.Navigated += RootFrame_Navigated;
        } 

        if (e.NavigationMode == NavigationMode.Back &&
            e.Uri.OriginalString.Contains("ThePage.xaml"))
        {
            RootFrame.Opacity = 0;
            _skipped = true;
        }
    }

The code to determine when to make the page transparent is also different in my actual code, but I've added an implementation to the answer for illustration. This is almost identical to the code in the recipe linked in another answer, but doesn't involve needing to use a third party DLL.

I provided my own answer because I've seen the sources provided in the other answers before, but never paid attention to the code for hiding the root frame. I am not using the Non-Linear Navigation Service, just the code fragment for frame transparency (I don't need it to detect circular navigation as I'm well aware of the design choices I make in the app and can spot them myself :-)

This suffices as a workaround in the (currently) one case I have where I need to skip a page that doesn't make sense when going back. I'd like to think when Mango comes out I will be best placed targeting the latest version, so this code will soon be defunct.

Sources:

There is no way to do it before Mango, except with the flickering way you already know.

In Mango, you can use NavigationService.RemoveBackEntry .

And it's more a question of a bad application design, than anything else. You shouldn't require a immediate page for anything.

Your workaround should be to design a application structure that doesn't require any immediate pages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM