Deep linking in Silverlight application

September 3, 2009

Often you want to do deep linking in Silverlight without going the whole hog and using the new Navigation Framework. Not that the Navigation Framework is bad, but it does impose a architecture on your application that you may not want to follow.

Instead you can use the HtmlPage.Window.CurrentBookmark to store an arbitrary string. This will change the address in the browser by appending the string as a hash code or anchor. This is great as you can now bookmark the URL by hitting Ctrl+D. Only problem is if a user selects a bookmark while you application is still running then there’s no obvious way to detect the change.

I found a couple of blog entries suggesing ways to do this. One suggested polling on a timer to check for the change. Another used a bunch of JavaScript and didn’t work. As you can imagine, neither of these is a great option. Finally after hours of googling I found that you can do:

HtmlPage.Window.AttachEvent(“onhashchange”, new EventHandler(OnHashChange));

Works like a charm!

Advertisements

One Response to “Deep linking in Silverlight application”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: