Solving Circular Navigation in Silverlight for Window Phone
This article explains the Windows Silverlight app "circular navigation problem", and introduces the NonLinear Navigation Services library which can be used to overcome it.
Article Metadata
Tested with
Compatibility
Article
Contents |
Introduction
In the "standard" Windows Phone Silverlight application model, users move linearly "forward" through pages and can then use the "back" button to retract through previously visited pages, and finally to exit. Windows Phone navigation history maintains a "last-in, first-out" structure called "back-stack". This provision makes it easy to implement a linear path forward and back through navigated pages.
However it is possible to directly navigate to an arbitrary URI from any page, and this can result in navigation to a URI that is already in the back-stack, creating a loop. This can result in confusing navigation for the end user. Consider for example, if the user can navigates to "Home" rather than unwinding the page stack as shown in the game Petualangan Aksara di Tanah Jawa below:
Users expect to exit the game when they press the back button from the home page, but because of the loop they will be taken back to a previously navigated "stage" page.
One solution to overcome this problem is to use a NonLinear Navigation Services[1] library developed by Windows Phone Recipes. This library automatically unwinds any loops if it detects that you're navigating to a page that is already in the back stack.
Using the library
The steps for using this library are straightforward:
- Download File:NonLinear-WP-SLApp-Navigation-Service.zip
- Add NonLinearNavigationServices project in the Solution which has been created.
- Initialize the service at the end of class constructor whose class is the inheritance of Application class.
NonLinearNavigationService.Instance.Initialize(RootFrame);
Summary
With NonLinear Navigation Services, developers can more easily develop applications because they do not need to waste a lot of time thinking about the navigation management of the application.
Good luck!
Reference
- ↑ Kiriaty, Yochay. 2010. Solving Circular Navigation in Windows Phone Silverlight Applications.



Contents
Hamishwillee - Subedited
Hi Jeffrey
Thanks very much for this translation. This is clearly useful for English programmers too!
I've subedited this for readability. What you had wasn't bad, but I believe it is now clearer what this article delivers.
IMO the most common circular issue this is to solve is ensuring that from Home you always exit the app when you press back (it is quite possible that other loops in the app are fine). If this is the only case you are worried about it might be better to simply force this navigation path from your home page (ie overload pressing of the back button to exit the app). Is this possible?
IF it is possible then my suggestion would be to add a section showing how to do this as well. This would then provide a number of options for developers, depending on what their navigation needs are.
Regards
Hamishhamishwillee 04:25, 2 October 2012 (EEST)
Jeffrey.Halimsetiawan - Answer
Hi Hamish,
Actually, this solution is used to overcome circular navigation in my game. I already tried to overload pressing of the back button and in that method, I call NavigationServices.GoBack() or force it to go to the correct page. But it just make confused to manage the navigation of my application. Therefore, I used this library so I just know which page should I go and this library will take the rest.
Regards,
JeffreyJeffrey.Halimsetiawan 10:36, 2 October 2012 (EEST)
Hamishwillee - I do understand that
Hi Jeffrey
I do understand that if you want to remove all loops in your software this is the easiest way to do so. However there will be cases where you don't want to untangle "all" loops - you just want to ensure that selecting back from the home page will close close the app. In this case, you could just overload back on the home page - right?
So I like what you've done and I see that it makes perfect sense for your app. My suggestion is that for some apps just overloading back on the home page would be easier and sufficient for their navigation.
Regards
Hamishhamishwillee 04:16, 3 October 2012 (EEST)
Jeffrey.Halimsetiawan - Answer
Hi Hamish,
From your explanation, I don't understand with this sentence "However there will be cases where you don't want to untangle "all" loops". Could you please give me example of that navigation? Because I think that as long as we have "loop", we could just overload back keypress method but we need to do that in each loop page in order to make sure that it goes to correct page in application back-stack.
This is what I got from your explanation: - MainPage
NavigationService.Navigate(new Uri("/GamePage.xaml", UriKind.Relative));- GamePage
NavigationService.Navigate(new Uri("/StagePage.xaml", UriKind.Relative));- StagePage
protected virtual void OnBackKeyPress(CancelEventArgs e ) { NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative)); }- MainPage
protected virtual void OnBackKeyPress(CancelEventArgs e ) { NavigationService.GoBack(); }Sorry if there is a misunderstanding and please correct me if I'm wrong.
Thanks.Jeffrey.Halimsetiawan 05:27, 3 October 2012 (EEST)
Hamishwillee - Hmmm, not sure how I can better explain
Hi Jeffrey
I'm not sure how I can better explain. I will try below, but if it doesn't make sense, just ignore me and I'll delete these comments.
OK, so in your example you always want to remove all loops, and you also want to exit if back is pressed in mainpage. However what if you have an app where you (still) want to exit if back is pressed on mainpage, but you do NOT want to remove (other) loops in the navigation?
My proposed solution would be to override OnBackKeyPress (in MainPage only) with a call that exits the app (like "app.Exit()", if such a method exists). Then any other loops are followed, but if you press back in MainPage the app exits. Its a simpler implementation if you don't care about loops other than ones that might stop you exiting in MainPage
Does that make sense? As above, if not then lets forget it.
Regards
Hhamishwillee 07:14, 4 October 2012 (EEST)
Jeffrey.Halimsetiawan - Not possible
Hi Hamis,
Now, I get your point. And, I think it doesn't make sense. As far as I know, there is no method that can be used to exit application like app.Exit(). The only way to exit the application is to clear the application back-stack. So, you must use GoBack() until reach the first page you've visited in your app.
That's way I don't understand what do you mean. Sorry for the misunderstanding.
Thanks.Jeffrey.Halimsetiawan 12:44, 4 October 2012 (EEST)
Hamishwillee - Thanks very much for your patience
Hi Jeffrey
Thanks very much. Sorry for wasting your time on something "impossible". The fact you can't arbitrarily exit an app is useful for me to know!
Cheers
Hhamishwillee 06:39, 5 October 2012 (EEST)