开发者

Adding a C# enum value to a Navigation URI

开发者 https://www.devze.com 2023-01-09 15:17 出处:网络
I\'m developing a Windows Phone application. I make the question here because I think a silverlight question.

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 d开发者_StackOverflow中文版o 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`...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消