【问题标题】:Bing maps binding trouble必应地图绑定麻烦
【发布时间】:2011-09-07 00:22:38
【问题描述】:

将我的物品绑定到 silverlight bing 地图上的图钉上,我真的很头疼。

我花了一整天的时间来整理我的收藏,现在却无法让图钉显示出来。

当您根据下图在最后一行执行断点时,这些项目似乎在那里,所有 143 个项目都在 _PushPins 中:

欢迎任何帮助。非常感谢。

代码如下:

 namespace observable_collection_test
{
public partial class Map : PhoneApplicationPage
{

    public Map()
    {
        InitializeComponent();

        GetItems();
    }

    private ObservableCollection<SItem2> pushPins; 
    public ObservableCollection<SItem2> PushPins 
    {   
        get  { return this.pushPins; }   
        set   
        {     
         this.pushPins = value;

        this.OnPropertyChanged("PushPins");   
         } 
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

    }


 public void GetItems()
    {

         var document = XDocument.Load("ListSmall.xml");

         if (document.Root == null)
            return;

          var xmlns = XNamespace.Get("http://www.blah");

         var events = from ev in document.Descendants("item")
          select new
          {
           Latitude = Convert.ToDouble(ev.Element(xmlns + "Point").Element(xmlns + "lat").Value),
            Longitude = Convert.ToDouble(ev.Element(xmlns + "Point").Element(xmlns + "long").Value),

         };

         this.PushPins = new ObservableCollection<SItem2>();   
         foreach (var ev in events)   
          {     
         var pushPin = new SItem2(ev.Latitude, ev.Longitude);
         //Location = new GeoCoordinate(ev.Latitude, ev.Longitude )
         this.PushPins.Add(pushPin);   
         } 
}

其他类:

namespace observable_collection_test
{
 public class SItem2
{
           public double Latitude
    { get; set; }
    public double Longitude
    { get; set; }

    public SItem2(double Latitude, double Longitude)
    {
                    this.Latitude = Latitude;
        this.Longitude = Longitude;
              }

    public Location Location { get; set; }
}
}

XAML:

 <my:Map ZoomBarVisibility="Visible" ZoomLevel="10" CredentialsProvider="xxxxx"  Height="508" HorizontalAlignment="Left" Margin="0,22,0,0" Name="map1" VerticalAlignment="Top" Width="456" ScaleVisibility="Visible">
                  <my:MapItemsControl ItemsSource="{Binding PushPins}" >
                <my:MapItemsControl.ItemTemplate>
                    <DataTemplate>        
             <my:Pushpin Background="Aqua" Location="{Binding Location}" ManipulationCompleted="pin_click">
                        </my:Pushpin></DataTemplate>
                </my:MapItemsControl.ItemTemplate>
            </my:MapItemsControl>
 </my:Map>

【问题讨论】:

    标签: c# silverlight windows-phone-7 observablecollection bing-maps


    【解决方案1】:

    您正在尝试绑定到私有字段 _PushPins,而不是公共属性 PushPins。如果您在绑定后更改集合,也不要忘记实现 INotifyPropertyChanged。

    【讨论】:

    • 感谢您的回答马丁。我已将代码和图像编辑为我认为可能存在的问题。仍然没有喜悦。我是否需要一些公共 ObservableCollection PushPins 的设置访问器?
    • 是的,您只能绑定到属性,不能绑定到字段。此外,您的 XAML 仍在引用 _PushPins。
    • 啊,使用 XAML 是一个错误。小学生错误。我不完全确定我在那里使用 get/set 访问器做了什么,我对其进行了编辑,当然仍然没有乐趣:(我确信这对专家来说很明显这里有什么问题:-)
    • 您是否遇到运行时错误?在您的 PushPins getter 中,您将返回属性值本身,这将导致 StackOverflowException。
    【解决方案2】:

    我会做两件事 - 首先,实现 INotifyPropertyChanged 并让您的 PushPins 属性使用私有支持字段。在 getter 中,只需返回字段值,在 setter 中,更新字段值并调用 PropertyChanged 事件。

    然后,在您的循环中,实例化 PushPins 集合并直接填充它,而不是创建本地 ObservableCollection 的 getItems 方法(我会使用 PascalCase 作为方法名称)。

    private ObservableCollectin<SItem2> pushPins;
    public ObservableCollection<SItem2> PushPins
    {
      get { return this.pushPins; }
      set
      {
        this.pushPins = value;
        this.OnPropertyChanged("PushPins");
      }
    }
    
    public void GetItems()
    {
      ...
    
      this.PushPins = new ObservableCollection<SItem2>();
      foreach (var ev in events)
      {
        var pushPin = new SItem2(ev.Latitude, ev.Longitude);
        this.PushPins.Add(pushPin);
      }
    }
    

    在 SItem2 构造函数中填充 SItem2 Location 属性,而不是使用纬度/经度,正如 Martin 所说,如果您希望 UI 在更改集合中 SItem2 实例的值时更新,然后在 SItem2 的属性上实现 INotifyPropertyChanged也。

    【讨论】:

    • 感谢您迄今为止的帮助。我已将代码更新为我当前的游戏状态。我不确定我在用“Location = new GeoCoordinate(ev.Latitude, ev.Longitude)”做什么来构建位置项目?我确定我缺乏知识在这里出现..
    • 您没有在任何地方设置 Location 值,在您的 SItem2 构造函数中您可以设置 this.Location = new Location(this.Latitude, this.Longitude);
    猜你喜欢
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    • 2015-07-15
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-05
    相关资源
    最近更新 更多