开发者

How to add Pushpin Onclick Event?

I am using the below code to disaply multiple pushpins from an XML file. I would like to know how I setup an tap event for each pushpin that will pass a value

    foreach (var root in Transitresults)
    {
        var accentBrush = (Brush)Application.Current.Resources["PhoneAccentBrush"];

        var pin = new P开发者_JS百科ushpin
        {
            Location = new GeoCoordinate
                {
                    Latitude = root.Lat,
                    Longitude = root.Lon
                },
            Background = accentBrush,
            Content = root.Name
        };

        BusStopLayer.AddChild(pin, pin.Location);

    }
}


What you have is pretty close, try this:-

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


From MSDN page for PushPin events you can see that Tap event is available and so you can register a handler:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜