问题
Greetings,
Searched for this, but no luck.
I'm attempting to pass query string variables (and then retrieve them) in my Silverlight 4 app. First I tried this
this.NavigationService.Navigate(new Uri("/LoanProductionRegion?Elvis=Alive&ImHungry=true", UriKind.Relative));
But HtmlPage.Document.QueryString doesn't pick them up because they come after the anchor (the full url looks like http://localhost:1076/Dashboard.SLTestPage.aspx#/LoanProductionRegion?Elvis=Alive&ImHungry=true).
I tried to put the vars in front, such as
this.NavigationService.Navigate(new Uri("?Elvis=Alive&ImHungry=true/LoanProductionRegion", UriKind.Relative));
this.NavigationService.Navigate(new Uri("/?Elvis=Alive&ImHungry=true/LoanProductionRegion", UriKind.Relative));
But both result in a "Page not found" error. Anyone know how to achieve what I'm after?
回答1:
You can use the OnNavigatedTo method in your page, and then inspect this.NavigationContext.QueryString["Elvis"] (or whatever other parameter name you like).
NavigationContext (much like NavigationService) is guaranteed to be initialized by the time OnNavigatedTo is called, so that's the most convenient time to inspect it in most cases.
NavigationContext.QueryString is simply an IDictionary that has pre-parsed the key-value pairs in the query string. As such, you can do anything you expect with an IDictionary - use ContainsKey to see if something is present, iterate over it in a foreach loop, etc.
来源:https://stackoverflow.com/questions/4555985/how-can-i-pass-query-string-variables-with-navigationservice-navigate