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!