WP8 -如何通过选定项目更改MapCartographicMode
本文关键字:项目 MapCartographicMode 何通过 WP8 | 更新日期: 2023-09-27 17:51:15
我有这个列表框:
<ListBox x:Name="layerMenu" SelectionChanged="layerMenu_SelectionChanged" >
<ListBoxItem Content="Road" HorizontalAlignment="Center"/>
<ListBoxItem Content="Aerial" HorizontalAlignment="Center" />
<ListBoxItem Content="Hybrid" HorizontalAlignment="Center" />
<ListBoxItem Content="Terrain" HorizontalAlignment="Center" />
</ListBox>
和我想改变我的地图的制图模式。
private void layerMenu_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
MapCartographicMode selected = (MapCartographicMode)((sender as ListBox).SelectedItem);
MyMap.CartographicMode = selected;
}
这个错误发生:
System.InvalidCastException: Specified cast is not valid.
我可以通过使用开关(layerMenu.SelectedIndex)来解决这个问题,但如果可能的话,我更喜欢这种更短的方式。谢谢你的帮助。
修改
MapCartographicMode selected = (MapCartographicMode)((sender as ListBox).SelectedItem);
MyMap.CartographicMode = selected;
:
ListBox listbox = (sender as ListBox)
ListBoxItem item = (ListBoxitem)listbox.SelectedItem;
MapCartographicMode selected = (MapCartographicMode)Enum.Parse(typeof(MapCartographicMode), item.Content.ToString());
MyMap.CartographicMode = selected;