Handling tap on Pushpin in a fixed Map

匆匆过客 提交于 2019-12-11 11:50:08

问题


I have a Map control showing a few Pushpins. I do not want the user to navigate in the map so I disable it. But I do want the user to be able to tap on a Pushpin (and in the event I navigate to a detail page).

However when the Map.IsEnabled is false, the Pushpins don't seem to receive any gestures either. I've also tried using IsHitTestVisible, but with no luck.

Some code showing what I'm trying to do. Does anyone have any ideas?

<maps:Map Name="Map"
          VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
          CopyrightVisibility="Collapsed" LogoVisibility="Collapsed" ScaleVisibility="Collapsed" ZoomBarVisibility="Collapsed"
          IsEnabled="False">
    <maps:MapItemsControl ItemsSource="{Binding TheCollection}">
        <maps:MapItemsControl.ItemTemplate>
            <DataTemplate>
                <maps:Pushpin Name="Pin" Location="{Binding Coordinate}" Content="{Binding Ix}">
                    <maps:Pushpin.Background>
                        <SolidColorBrush Color="{StaticResource PhoneAccentColor}"/>
                    </maps:Pushpin.Background>

                    <toolkit:GestureService.GestureListener>
                        <toolkit:GestureListener Tap="PinTap"  />
                    </toolkit:GestureService.GestureListener>
                </maps:Pushpin>
            </DataTemplate>
        </maps:MapItemsControl.ItemTemplate>
    </maps:MapItemsControl>
</maps:Map>

回答1:


Setting IsEnabled to false prevents the Map control from responding to user input, which affects the child Pushpin as you've seen. If you want the map to be read-only but the Pushpin to respond to gestures then I think you have two options:

  1. Handle all the gesture events on the Map control and set e.Handled to true, which will prevent the Map itself from processing the event, but should leave the PushPin free to handle the tap gesture.
  2. Create a WriteableBitmap of the Map and show that instead, and then display the Pushpin on top (NOTE: I suspect that the Pushpin control won't work outside of the Map control, so you'd need to create/re-template a control to look like a Pushpin).

UPDATE: The events that you need to handle on the Map to make it appear "read-only" but remain enabled are MapPan and MapZoom.




回答2:


So here's how I solved it after a lot of testing and browsing MSDN. It turns out that things are a bit different in the Map control on Windows Phone (see MSDN). There are new behaviors and events compared to normal Silverlight.

<maps:Map Name="Map"
          VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
          CopyrightVisibility="Collapsed" LogoVisibility="Collapsed" ScaleVisibility="Collapsed" ZoomBarVisibility="Collapsed"
          MapZoom="Map_MapZoom" MapPan="Map_MapPan">
    <maps:MapItemsControl ItemsSource="{Binding TheCollection}">
        <maps:MapItemsControl.ItemTemplate>
            <DataTemplate>
                <maps:Pushpin Name="Pin" Location="{Binding Coordinate}" Content="{Binding Ix}">
                    <maps:Pushpin.Background>
                        <SolidColorBrush Color="{StaticResource PhoneAccentColor}"/>
                    </maps:Pushpin.Background>

                    <toolkit:GestureService.GestureListener>
                        <toolkit:GestureListener Tap="PinTap"  />
                    </toolkit:GestureService.GestureListener>
                </maps:Pushpin>
            </DataTemplate>
        </maps:MapItemsControl.ItemTemplate>
    </maps:MapItemsControl>
</maps:Map>

...

private void Map_MapPan(object sender, MapDragEventArgs e)
{
    e.Handled = true;
}

private void Map_MapZoom(object sender, MapZoomEventArgs e)
{
    e.Handled = true;
}


来源:https://stackoverflow.com/questions/4864243/handling-tap-on-pushpin-in-a-fixed-map

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