在生成PushPin之后,获取此可能的非预期参考比较下划线消息

时间:2012-07-03 14:21:48

标签: windows-phone-7

我有此错误消息:

Possible unitended reference comparison; to get a value coparison, cast the left hand side to type string

problem :
((Pushpin)p).Tag == "locationPushpin"));

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

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

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

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

         if (pushpin != null)
          {
              this.map1.Children.Remove(pushpin);
          }
      }

     Pushpin locationPushpin = new Pushpin();

     //---set the location for the pushpin---

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

     locationPushpin.Content = new Ellipse()
     {
        Fill = new SolidColorBrush(Colors.Orange),
        //Opacity = .8,
        Height = 40,
        Width = 30
      };

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

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

非常感谢您的帮助。感谢

1 个答案:

答案 0 :(得分:2)

首先,您的查询只会找到类型为Pushpin完全对象。这更清洁:

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

下一个问题是Tag的类型为object。所以你真的想要:

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

否则,您将在Tag值和字符串之间进行引用比较。所以你可以拥有相同但不同的字符串,并且找不到图钉。