Bing 地图绑定问题
我真的很头疼,将我的物品绑定到 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>
Im having a real headache binding my items to pushpins on a silverlight bing map.
Ive spent all day trying to get my collection sorted and now just cant get the pushpins to show up.
The items appear to be there as when you do a breakpoint on the last line as per the image below, all 143 items are there in _PushPins:
Any help welcome. many thanks.
Here is the code:
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);
}
}
Other class:
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您尝试绑定到私有字段 _PushPins,而不是公共属性 PushPins。如果您在绑定后更改集合,也不要忘记实现 INotifyPropertyChanged。
You're trying to bind to a private field, _PushPins, instead of the public property PushPins. Also don't forget to implement INotifyPropertyChanged if you're changing the collection after binding.
我会做两件事 - 首先,实现 INotifyPropertyChanged 并让您的 PushPins 属性使用私有支持字段。在 getter 中,仅返回字段值,在 setter 中,更新字段值并调用 PropertyChanged 事件。
然后,在您的循环中,不是使用 getItems 方法(我将使用 PascalCase 作为方法名称)创建本地 ObservableCollection,而是实例化 PushPins 集合并直接填充它。
在 SItem2 构造函数中填充 SItem2 Location 属性,而不是使用纬度/经度,正如 Martin 所说,如果您希望 UI 在更改集合中的 SItem2 实例的值时更新,则还要在 SItem2 的属性上实现 INotifyPropertyChanged。
I would do two things - firstly, implemented
INotifyPropertyChanged
and have your PushPins property use a private backing field. In the getter, just return the field value, in the setter, update the field value and invoke the PropertyChanged event.Then, in your loop, rather than the getItems method (I would use PascalCase for method names) creating a local ObservableCollection, instantiate the PushPins collection and populate it directly.
Populate the SItem2 Location property in the SItem2 constructor instead using the lat/long, and as Martin says, if you want the UI to update when you change the value of an SItem2 instance in the collection, then implement INotifyPropertyChanged on SItem2's properties also.