如何添加Pushpin Onclick事件

本文关键字:Pushpin Onclick 事件 添加 何添加 | 更新日期: 2023-09-27 18:09:38

我使用下面的代码从XML文件中删除多个图钉。我想知道如何为每个图钉设置一个tap事件,该事件将传递一个值

    foreach (var root in Transitresults)
    {
        var accentBrush = (Brush)Application.Current.Resources["PhoneAccentBrush"];
        var pin = new Pushpin
        {
            Location = new GeoCoordinate
                {
                    Latitude = root.Lat,
                    Longitude = root.Lon
                },
            Background = accentBrush,
            Content = root.Name
        };
        BusStopLayer.AddChild(pin, pin.Location);
    }
}

如何添加Pushpin Onclick事件

你已经很接近了,试试这个:-

    foreach (var root in Transitresults)
    {
        var accentBrush = (Brush)Application.Current.Resources["PhoneAccentBrush"];
        var pin = new Pushpin
        {
            Location = new GeoCoordinate
                {
                    Latitude = root.Lat,
                    Longitude = root.Lon
                },
            Background = accentBrush,
            Content = root.Name,
            Tag = root
        };
        pin.MouseLeftButtonUp += BusStop_MouseLeftButtonUp;

        BusStopLayer.AddChild(pin, pin.Location);
    }
}
void BusStop_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    var root =  ((FrameworkElement)sender).Tag as BusStop;
    if (root != null)
    {
        // You now have the original object from which the pushpin was created to derive your
        // required response.
    }
}

从PushPin事件的MSDN页面,你可以看到Tap事件是可用的,所以你可以注册一个处理程序:

pin.Tap += args => { /* do what you want to do */ };