URI Mapper with query string doesn't seem to w

2019-08-01 09:22发布

问题:

I'm attempting to set up a URI mapper so that in the end, I can pass a query string to the xaml page I am loading.

Here's what I have defined in the XAML.

<navigation:Frame.UriMapper>
    <uriMapper:UriMapper>
         <uriMapper:UriMapping Uri="RequirementOverview/{id}" MappedUri="/View/Pages/RequirementOverview.xaml?id={id}" />
         <uriMapper:UriMapping Uri="/{pageName}" MappedUri="/View/Pages/{pageName}.xaml"/>
    </uriMapper:UriMapper>
</navigation:Frame.UriMapper>

My Intention is that if you click on a link such as '/RequirementOverview/Foo' "Foo" is passed as a query string to the Requirement page, and then we can do our magic with it.

However when calling Fame.Navigate("RequirementOverview/Foo", UriKind.Relative) I always end up at the page like thus: hostpage.aspx#/RequirementOverview/Foo and no query is passed to the page. Rather, it seems to work (the navigation), but my navigationContext queryString comes back null.

Is my approach for this incorrect? Thanks in advance.

回答1:

The URL that you get in the bowser (hostpage.aspx#/RequirementOverview/Foo) is what you should expect since you're using mapped URIs.

To get the QueryString parameters, all you have to do is override the OnNavigatedTo of your page, access the QueryString (which is a Dictionary<string,string>) property of the NavigationContext which is a member of the Page class, like this:

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    try
    {
       var id = int.Parse(NavigationContext.QueryString["id"];
    }
    catch{}
}

Hope this helps :)