I’ve seen several other questions based upon this but no definitive answers when dealing with Silverlight.
I’m using Silverlight 5 and the navigation framework to navigate between different portions of my application. What I’d like to do is provide deep linking into my application as the user performs actions I’d like to update the url.
I know I can do this by calling NavigationService.Navigate but doing so will reinitialize that entire page and my viewmodel when all I really need to do is allow the user to refresh straight to that view or save a direct link from the address bar.
I know I should be able to use javascript to do this but I’d rather not if there’s a way to do this within Silverlight that anybody knows about.
Further Clarification
One of my pages is a report. This report already has paging functionality. I like to be able to update the URL to #MyReport?Page=2 without actually reloading the page (as like I said paging is already taken care of). I would only need to handle the querystring in the OnNavigatingTo function without fully reloading the page every time they click on something. This would allow the user to copy that Url and put it into an email or wherever they want to put it and other users would go directly to that.
I don’t see this documented anywhere but when you manually add a hash to the url, it shows up in the address bar as a dollar sign.
Thus, if I load in to the page:
/MyReportthe navigation framework puts it into the url bar via the UriMapper as#/MyReportand when they click to go to the next set of data within the report (or filter, or whatever else I decide to make navigable this way) I can tell the navigation service to go to/MyReport#page=2&otherkey=othervalueand the navigation framework will change this behind the scenes to be#/MyReport$page=2&otherkey=othervalueand this WILL NOT reload the page.I can then add my custom functionality in the
OnNavigatedToto add extra initialization. When I go to react to this value within the code, it changes that dollar sign back to a hash withinNavigationService.CurrentSource.I believe this is because silverlight is reacting to the “hashchanged” event within the browser and navigating appropriately. It seems to handle the dollar sign as a way to allow the developer to add a hash without screwing up the navigation.
Update
Even though this modifies the url there’s no way to update it based upon the forward/backward navigation. Since the navigation framework is rewriting my # to a $ it’s not doing any navigation as for all intents and purposes we’re still on the same page. Hitting the browser forward/backward will go to the last url but will not trigger any changes so I have to do this manually. This can be done within the
FragmentNavigationevent on theNavigationServiceobject within the page. When handled appropriately this will allow forward/backward navigation within the same Silverlight page.This solution allows for both deep-linking and additional forward/backward navigation without constantly re-initializing page and viewmodel.