0

我有这个错误信息:

可能的统一参考比较;要获得一个值比较,请将左侧转换为字符串

问题 :
((Pushpin)p).Tag == "locationPushpin"));

==============================

双 Dlat = Convert.ToDouble(g_strLat);            
双 Dlon = Convert.ToDouble(g_strLon);

 this.map1.Center = new GeoCoordinate(Dlat, Dlon);           

  如果 (this.map1.Children.Count != 0)
     {
        var pushpin = map1.Children.FirstOrDefault(p => (p.GetType() == typeof(Pushpin) && ((Pushpin)p).Tag == "locationPushpin"));

         如果(图钉!= null)
          {
              this.map1.Children.Remove(图钉);
          }
      }

     图钉位置图钉 = 新图钉();

     //---设置图钉的位置---

      locationPushpin.Tag = "locationPushpin";
      locationPushpin.Location = new GeoCoordinate(Dlat, Dlon);

     locationPushpin.Content = 新椭圆()
     {
        填充 = 新的 SolidColorBrush(Colors.Orange),
        //不透明度 = .8,
        高度 = 40,
        宽度 = 30
      };

      locationPushpin.Width = 60;
      locationPushpin.Height = 100;

      this.map1.Center = new GeoCoordinate(Dlat, Dlon);
      this.map1.Children.Add(locationPushpin);
      this.map1.ZoomLevel = 13;  

感谢您的帮助。谢谢

4

1 回答 1

1

首先,您的查询只会找到type 的确切对象Pushpin。这更干净:

var pushpin = map1.Children.OfType<Pushpin>()
                           .FirstOrDefault(p => p.Tag == "locationPushpin");

下一个问题是Tagtype object。所以你真的想要:

var pushpin = map1.Children.OfType<Pushpin>()
                           .FirstOrDefault(p => "locationPushpin".Equals(p.Tag));

Otherwise you'll be doing a reference comparison between the Tag value and the string. So you could have equal but distinct strings, and the pushpin wouldn't be found.

于 2012-07-03T14:24:53.033 回答