3

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 C I 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.

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187

3 Answers3

3

You should read this blog post on solving circular navigation issues and you can download this recipe code which demonstrates non-linear navigation.

keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • Have you used this before? All it appears to do is go back through the pages for me, I've already got the code doing that. Does it display the pages it is quickly backing through? – Adam Houldsworth Aug 18 '11 at 15:28
  • it appears to make RootFrame transparent. – Adam Houldsworth Aug 18 '11 at 15:30
  • No, I've never needed it, but I've seen it suggested quite a few times for the scenario you're describing. – keyboardP Aug 18 '11 at 15:49
  • +1 I've seen the recipe before but only glossed through it's implementation. Seeing it again, I noticed the frame transparency handling which led to my eventual solution. – Adam Houldsworth Aug 18 '11 at 16:00
  • I think you should mark your answer as the correct one (when you can) and have this one supplement it for anyone else reading this thread. (Or could edit your answer to include the links to the recipe for context) – keyboardP Aug 18 '11 at 16:02
3

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:

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
1

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.

Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150
  • How so? Page A is my home page, the user clicks "new item", B is the new item entry page, taking you to C, the listing page for that item... where is the bad design? I also understand there is no supported way to do it (hence my talk of workarounds) and that I know of the Mango support... – Adam Houldsworth Aug 18 '11 at 15:19
  • I'm sorry, what do you mean by "immediate page"? – Adam Houldsworth Aug 18 '11 at 15:21
  • And why wouldn't the user want to return to the "new entry" page then? If Page B is a creation/dialog-like page, you should return the user to the "home" screen after creation, not redirect them to a listing page. If the user want to see the listings, they should either be on your home screen, or the user should navigate there himself. – Claus Jørgensen Aug 18 '11 at 15:22
  • You never told what page B was. A "immediate page" means a "in between" page in the context of navigation. – Claus Jørgensen Aug 18 '11 at 15:23
  • the home page is simply a list of all top level items. A new item is then created and the user accepts. Instead of taking back to the listing of top level items, it is highly likely that the user will want to instead drill into that item (page C) so navigation goes forwards. I spotted the problem long before I ran into the code and decided to work around it. I argue it isn't a design fault if they have introduced support for it in later versions. – Adam Houldsworth Aug 18 '11 at 15:24