Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does NavigationService on Silverlight/WP7 uses Strings over Classes?

Given that C# is a leans more towards a strongly typed language, why did the designers chose navigation based on URIs over Classes?

NavigationService.Navigate(new Uri("/MyPage.xaml", UriKind.Relative)) 

fails at run time if MyPage is missing.

If there were a method that support passing a PhoneApplicationPage as a argument like

NavigationService.Navigate(new MyPage()); 

Navigation related errors can be caught at compile time.

Why was this not an inherent design in Silverlight/WP7 ?

like image 308
Mugunth Avatar asked May 18 '11 04:05

Mugunth


2 Answers

Only the WP7 dev tools team can say for sure, but absent their input, I'll give it my best shot. My guess is that it arises out of using a plug-in framework for client application development. Web Silverlight doesn't even really have the concept of navigation. You can switch frames in and out of the applications main frame, but that's not really navigation persay. So, when they were asked to use Silverlight as a client application tool for WP7, they had to decide how they were going to navigate around.

The most obvious way to solve the navigation problem, if you're a team that's been building internet applications, is to make something as close to the internet model as possible. This allows developers to carry internet applications to the browser and do minimal rewrites of their code. Think about it, if you had a web application that used relative URLs to move between application pages, your WP7 could reuse most of that navigation logic with only slight modifications to use the new NavigationService. This also minimizes the changes from core Silverlight that the WP7 version has to make, making everything easier to keep synchronized between platforms.

It obviously isn't the best way of doing things, and it makes state maintenance between pages a royal pain in the ass, but I can at least see why they decided to do it this way. The main flaws, in my experience, are the runtime checking of navigation destinations (instead of compile time with classes), passing parameters (every page needs an OnNavigatedTo that parses out variables) and maintenance of non-trivial objects between pages (I've taken to just using Singletons to hold relevant objects as a sort of poor-man's cache). I'm hoping for at least some modification to the navigation paradigm in 7.5 or 8, but I'm not holding my breath.

like image 113
Jared Harding Avatar answered Oct 15 '22 23:10

Jared Harding


This navigation model was inherited from Silverlight on the desktop (and WPF before it). It's important to note: this is not a String-based model, but rather a URI-based model. The difference here is key: we're not talking about an arbitrary string to point to some XAML, we're talking about a universal locator for a resource that is a page within the application. By addressing navigation this way, your application actually has more than one entry point -- any URI within the application can be a valid entry point. And by making navigation URI-based, you ensure that the "state" of your application as it relates to what you're looking at can be serialized, accessed at any time from any direction, etc.

Imagine, for example, that you have a link on a webpage (or in an email, or anywhere else) that you want to open in your application. Click the link, and because the URI fully describes the resource that the application should display (e.g. an item in a catalog, filters on a search, etc.), you can jump straight to it (a.k.a. deep-linking). This isn't implemented on Windows Phone 7 (at least not from other apps, but it's really how the back button, etc. work), but the model comes straight from Silverlight on the desktop (the Navigation framework is in the Silverlight SDK), and you can see where they might take it on Windows Phone in the future.

Again, the power of the URI is its universality -- it is a common way to identify a resource. Without it, you're stuck with a tight coupling of anything that wants to navigate into your application and the application itself.

like image 33
depoll Avatar answered Oct 15 '22 22:10

depoll