如何添加Pushpin Onclick事件?

时间:2011-09-23 08:26:36

标签: c# silverlight windows-phone-7 bing

我使用以下代码从XML文件中显示多个推针。我想知道如何为每个将传递值的图钉设置点击事件

    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);

    }
}

2 个答案:

答案 0 :(得分:1)

你所拥有的非常接近,试试这个: -

    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.
    }
}

答案 1 :(得分:0)

MSDN page对于PushPin事件,您可以看到Tap事件可用,因此您可以注册处理程序:

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