How can I pass query string variables with NavigationService.Navigate?

。_饼干妹妹 提交于 2020-01-02 11:22:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!