C# 如何添加图钉Onclick事件?

C# 如何添加图钉Onclick事件?,c#,silverlight,windows-phone-7,bing,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 Ge

我使用下面的代码从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);

    }
}

您所拥有的非常接近,请尝试以下方法:-

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

您所拥有的非常接近,请尝试以下方法:-

    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.
    }
}
从for PushPin events中,您可以看到Tap事件可用,因此您可以注册处理程序:

pin.Tap += args => { /* do what you want to do */ };
从for PushPin events中,您可以看到Tap事件可用,因此您可以注册处理程序:

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

我想你忘了注册事件处理程序-我没有看到pin。单击+=…@KarelFrajtak,单击事件用于错误的代码。我把它拿走了。即使对于这段代码,我如何添加onclick呢?我想你忘了注册事件处理程序-我没有看到pin。单击+=…@KarelFrajtak表示Click事件用于错误的代码。我把它拿走了。即使对于这段代码,我如何添加onclick呢?谢谢,但它不像MouseLeftButtonUp+=BusStop\u MouseLeftButtonUp那样适用于WP7吗?无效的初始值设定项成员声明您不能在初始化中注册事件处理程序,但它是点-事件已注册。@KarelFrajtak抱歉,您能解释一下您的意思吗?首先-您的代码没有注册任何事件处理程序,其次-您只能在初始值设定项块中初始化属性(代码用{和}包装)就在“新图钉”之后@KarelFrajtak是的,因为我不知道如何为图钉点击或按下注册事件处理程序。这就是为什么我要问这个问题。谢谢你,但它不像MouseLeftButtonUp+=bustop\u MouseLeftButtonUp那样适用于WP7吗?无效的初始值设定项成员声明您不能在初始化中注册事件处理程序,但它是点-事件已注册。@KarelFrajtak抱歉,您能解释一下您的意思吗?首先-您的代码没有注册任何事件处理程序,其次-您只能在初始值设定项块中初始化属性(代码用{和}包装)就在“新图钉”之后@KarelFrajtak是的,因为我不知道如何为图钉点击或按下注册事件处理程序。这就是我问这个问题的原因。但是请注意,
Tap
仅在7.1上可用。Pre-mango API没有
Tap
.google more->,但请注意
Tap
仅在7.1上可用。Pre mango API没有
点击
。谷歌搜索更多->