图钉点击事件

时间:2012-09-24 13:20:13

标签: windows-phone-7 silverlight-4.0 bing-maps pushpin

我有一个图钉集群,我已经为foreach中的所有图钉添加了点击事件。现在我需要找出点击了哪个图钉,以便相应地执行操作。以下是我的示例代码。

 private void setpins()
 {
            Pushpin pin = null;    

            lstpin.Add(new clsPushpin() { stores = "chennai", _loc= new locations() { lat = 13.04, longd = 80.17 } });
            lstpin.Add(new clsPushpin() { stores = "Karur", _loc = new locations() { lat = 10.58, longd = 78.07 } });
            lstpin.Add(new clsPushpin() { stores = "coimbatore", _loc = new locations() { lat = 11.00, longd = 77.00 } });

            foreach (clsPushpin cls in lstpin)
            {
                pin = new Pushpin();
                GeoCoordinate geo = new GeoCoordinate(cls._loc.lat, cls._loc.longd);
                pin.Location = geo;
                pin.Background = new SolidColorBrush(new Color() { A = 255, R = 0, G = 100, B = 120 });
                pin.Foreground = new SolidColorBrush(Colors.White);
                mymap.Children.Add(pin);
                pin.MouseLeftButtonUp += new MouseButtonEventHandler(pushpintap);
            }
            mymap.Center = pin.Location;
            mymap.SetView(pin.Location, 5.0);
  }

  private void pushpintap(object sender, MouseButtonEventArgs e)    
  {
      //Messagebox are what ever    
      MessageBox.Show("My lat long is:"+lat,+long);    
  }

使用上面的代码段,保存最后一个图钉的值。但我想找到确切的引脚,选择通知/弹出适当的。 Thx提前。

3 个答案:

答案 0 :(得分:2)

您可以执行以下操作:

将clsPushpin对象存储在图钉标记属性中。

在图钉点击事件中,将sender.tag强制转换为clsPushpin对象,以获取该图钉的数据。

        foreach (clsPushpin cls in lstpin)
        {
            pin = new Pushpin();
            GeoCoordinate geo = new GeoCoordinate(cls._loc.lat, cls._loc.longd);
            pin.Location = geo;
            pin.Background = new SolidColorBrush(new Color() { A = 255, R = 0, G = 100, B = 120 });
            pin.Foreground = new SolidColorBrush(Colors.White);
            mymap.Children.Add(pin);
            pin.MouseLeftButtonUp += new MouseButtonEventHandler(pushpintap);
            pin.Tag = cls;
        }

然后在你的pushpintap事件处理程序中执行以下操作:

  private void pushpintap(object sender, MouseButtonEventArgs e)    
  {
  //Messagebox are what ever    
    clsPushpin cls = sender.tag as clsPushpin;

    MessageBox.Show("My lat long is:"+cls.lat.ToString()+","+cls.long.ToString());    
  }

您可能应该使用数据绑定将数据绑定到图钉集合。

答案 1 :(得分:1)

您应该查询sender事件中的pushpintap,以找出被点击的图钉的详细信息。

答案 2 :(得分:0)

我遇到了类似的问题并寻找解决方案我发现了你的问题。我所做的是定义一个动态创建MouseButtonEventHandler的方法。例如,如果要显示有关每个cls对象的信息,可以定义下一个方法:

    public MouseButtonEventHandler getHandler(clsPushpin cls)
    {
        return delegate(object sender, MouseButtonEventArgs e)
            {
                MessageBox.Show(cls.stores+" at "+cls._loc.lat+","+cls._loc.longd);
            };
    }

并在每次创建/添加Pushpin时调用它:

pin.MouseLeftButtonUp += getHandler(cls);

[我尝试直接执行此操作而未定义getHandler方法(即:pin.MouseLeftButtonUp += delegate(...)并且它无法正常工作]