我正在使用C#创建一个Windows应用商店应用,并需要一些方法来帮助解决地图上的图钉被点击的方法。 到目前为止,我创建了一个名为currentPin的Pushpin,它是我在XAML代码中创建的。我还在这里为Tapped事件做了一个名为pushpin_Tapped的参考。
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<bm:Map x:Name="MyMap" Credentials="{StaticResource BingMapsApiKey}" Holding="map_Held">
<bm:Map.Children>
<bm:Pushpin x:Name="pin" Tapped="pushpinTapped">
</bm:Pushpin>
</bm:Map.Children>
</bm:Map>
</Grid>
然后我将currentPin放在MainPage.xaml.cs代码中的OnNavigatedTo方法的当前位置。我还创建了这个图钉被点击的方法。在这个方法中,我显示了一个用于点击currentPin的对话框。
private async void pushpinTapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
{
var x = MapLayer.GetPosition(pin);
MessageDialog dialog = new MessageDialog("You are here " + x.Latitude + " " + x.Longitude);
await dialog.ShowAsync();
}
然后我有一个方法,当用户持有地图时,在地点上放置一个新图钉,代码如下:
private void map_Held(object sender, HoldingRoutedEventArgs e)
{
Debug.WriteLine("You held at" + DateTime.Now.ToString() + "" + e.GetPosition(MyMap));
var pos = e.GetPosition(MyMap);
Location location;
MyMap.TryPixelToLocation(pos, out location);
Pushpin pin = new Pushpin();
MyMap.Children.Add(pin);
MapLayer.SetPosition(pin, location);
}
我知道它可能正在盯着我,但是我正在消息如何获取一个消息框或对话框,或者在点击这些新引脚时发生任何事情。有人可以解决一些问题吗? 谢谢, 艾米
答案 0 :(得分:2)
private void map_Held(object sender, HoldingRoutedEventArgs e)
{
Debug.WriteLine("You held at" + DateTime.Now.ToString() + "" + e.GetPosition(MyMap));
var pos = e.GetPosition(MyMap);
Location location;
MyMap.TryPixelToLocation(pos, out location);
Pushpin pin = new Pushpin();
pin.Tapped += pushpinTapped; // <<<<<<=====LOOK AT THIS
MyMap.Children.Add(pin);
MapLayer.SetPosition(pin, location);
}
答案 1 :(得分:1)
在Map_Held中,添加已点击事件的处理程序...
private void map_Held(object sender, HoldingRoutedEventArgs e)
{
Debug.WriteLine("You held at" + DateTime.Now.ToString() + "" + e.GetPosition(MyMap));
var pos = e.GetPosition(MyMap);
Location location;
MyMap.TryPixelToLocation(pos, out location);
Pushpin newpin = new Pushpin();
newpin.Tapped += pushpin_Tapped;
MyMap.Children.Add(newpin);
MapLayer.SetPosition(newpin, location);
}
并在您的tapped事件处理程序中,将引脚的引用从pin
更改为以下内容。
private async void pushpinTapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
{
PushPin tappedpin = sender as PushPin; // gets the pin that was tapped
if(null == tappedpin) return; // null check to prevent bad stuff if it wasn't a pin.
var x = MapLayer.GetPosition(tappedpin);
MessageDialog dialog = new MessageDialog("You are here " + x.Latitude + " " + x.Longitude);
await dialog.ShowAsync();
}