简体   繁体   中英

Adding a C# enum value to a Navigation URI

I'm developing a Windows Phone application. I make the question here because I think a silverlight question.

I have defined the following Navigations URIs on App.xaml:

<!-- Navigation links-->
<nav:UriMapper x:Name="UriMapper">
    <nav:UriMapper.UriMappings>
        <nav:UriMapping Uri="/Destination" MappedUri="/Views/Tourism/Common.xaml?Type=1"/>
        <nav:UriMapping Uri="/Points" MappedUri="/Views/Tourism/Common.xaml?Type=2"/>
    </nav:UriMapper.UriMappings>
</nav:UriMapper>

And the following C# enum:

public enum TourismItemType
{
    Destination = 1,
    PointOfInterest = 2,
    Content = 3
}

I want to change the ' 1 ' on MappedUri="/Views/Tourism/Common.xaml?Type= 1 " with the value obtained from TourismItemType.Destination .

I think, I can do that:

And do it programatically, but is there any way to access the value represented by TourismType.Destination on XAML?

Thanks.

This can be easily achieved by passing the value of the enum as a string and then parsing it into an enum in the OnNavigatedTo event.

MappedUri="/Views/Tourism/Common.xaml?Type=PointOfInterest"

and then in common.xaml:

string selectedType = "";
if (NavigationContext.QueryString.TryGetValue("Type", out selectedType))
{
    var tit = Enum.Parse(typeof (TourismItemType), selectedType, true);

    // do something with `tit`...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM