Just managed to get myself into (and out of) a gnarly situation with Windows Phone. Here’s the scenario: I needed to execute some conditional logic to show different screens at start up in my Windows Phone app.  Basically, if the user was already logged in, bounce them to homepage; otherwise send them to the log in screen.

My first stab was to check for if they were logged in during the log-in screen and redirect them. Bad UX with that though: you can’t navigate until the NavigationService is available, which isn’t until the UI is loaded, which means the user briefly sees the login screen. Ugly.

Second stab was to add a page that simply displayed the splashscreen.jpg in which the conditional logic was executed.  So, I had a page with nothing but a splashscreen and then, in the OnNavigatedTo event, I checked to see if they were logged in.  Seemed to be working, until I discovered that the user could never navigate backward out of the app, because whenever they hit that page, the app attempted to navigated them forward. Lesson learned? Don’t add navigation logic to the OnNavigatedTo event or else backward navigation is hosed.

I then discovered Peter Torr’s excellent framework for redirecting an initial navigation.  I implemented this, which puts all this logic in the Navigating event of the application class. That solved my navigation problem, but introduced a new problem: because my application uses the Share Picker extensibility for Windows Phone, I needed access to the NavigationContext querystrings, provided by the static method that hangs off of the PhoneApplicationPage object. For the life of me, I couldn’t figure out how to access this class, since all you can get at is the PhoneApplicationFrame when in the application class.  But, then I learned that NavigationContext is just a wrapper around the URI and I could simply get the URI out of the NavigatingCancelEventArgs as e.Uri and process the querystrings myself.

Oh, one other thing to remember when messing around in the Navigating event: be sure to set e.Cancel to true or else your overridden navigation won’t ever happen.