C# 将图钉绑定到Bing地图
我正在做一个WP7项目,并尝试用一种特殊的方式将图钉bing映射到bing地图,但它就是不起作用。它似乎读取XML文件ok,因为它计算所有项目,然后加载地图,但只是将黑色图钉放在地图的右上角。如果您有什么想法,为什么不拆下图钉,我将不胜感激。非常感谢。 我想我已经包括了所有的相关代码C# 将图钉绑定到Bing地图,c#,silverlight,silverlight-4.0,windows-phone-7,bing-maps,C#,Silverlight,Silverlight 4.0,Windows Phone 7,Bing Maps,我正在做一个WP7项目,并尝试用一种特殊的方式将图钉bing映射到bing地图,但它就是不起作用。它似乎读取XML文件ok,因为它计算所有项目,然后加载地图,但只是将黑色图钉放在地图的右上角。如果您有什么想法,为什么不拆下图钉,我将不胜感激。非常感谢。 我想我已经包括了所有的相关代码 namespace maps_data_test_2 { public class LocationData { public Location Location { g
namespace maps_data_test_2
{
public class LocationData
{
public Location Location
{
get;
set;
}
public String CustomerName
{ get; set; }
public Int32 CustomerId
{ get; set; }
public LocationData()
{
this.Location = new Location();
}
}
/// <summary>
/// This class exposes IEnumerable, and acts as ItemsSource for
/// MapItemsControl
/// </summary>
public class LocationDataCollection : ObservableCollection<LocationData>
{
public bool IsDataSource
{
get
{
return true;
}
set
{
this.Load();
}
}
public LocationDataCollection()
{
}
public void Load()
{
Uri url = new Uri("http://www.equestrian-photo.com/CustomerData.xml", UriKind.Absolute);
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(url);
}
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
StringReader stream = new StringReader(e.Result);
XmlReader reader = XmlReader.Create(stream);
Double Lat = 0.00;
Double Lng = 0.00;
String CustomerName = "";
Int32 CustomerId = 0;
LocationDataCollection locationList = new LocationDataCollection();
while (reader.Read())
{
reader.MoveToContent();
if (reader.NodeType == XmlNodeType.Element)
{
reader.MoveToFirstAttribute();
}
if (reader.NodeType == XmlNodeType.Attribute)
{
if (true == reader.MoveToAttribute("Latitude"))
{
Lat = reader.HasValue ? Convert.ToDouble(reader.Value) : 0.00;
}
if (true == reader.MoveToAttribute("Longitude"))
{
Lng = reader.HasValue ? Convert.ToDouble(reader.Value) : 0.00;
}
if (true == reader.MoveToAttribute("CustomerName"))
{
CustomerName = reader.Value.ToString();
}
if (true == reader.MoveToAttribute("CustID"))
{
CustomerId = Convert.ToInt32(reader.Value);
}
LocationData T = new LocationData();
T.Location.Latitude = Lat;
T.Location.Longitude = Lng;
T.CustomerName = CustomerName;
locationList.Add(T);
}
}
IEnumerator ppEnum = locationList.GetEnumerator();
while (ppEnum.MoveNext())
{
this.Add((LocationData)ppEnum.Current);
}
reader.Close();
}
}
}
名称空间映射\u数据\u测试\u 2
{
公共类位置数据
{
公共场所
{
收到
设置
}
公共字符串客户名称
{get;set;}
公共Int32客户ID
{get;set;}
公共位置数据()
{
this.Location=新位置();
}
}
///
///此类公开IEnumerable,并充当的ItemsSource
///MapItemsControl
///
公共类LocationDataCollection:ObservableCollection
{
公共布尔IsDataSource
{
收到
{
返回true;
}
设置
{
这个.Load();
}
}
公共位置数据收集()
{
}
公共空荷载()
{
Uri url=新的Uri(“http://www.equestrian-photo.com/CustomerData.xml“,乌里金。绝对);
WebClient客户端=新的WebClient();
client.DownloadStringCompleted+=新的DownloadStringCompletedEventHandler(client\u DownloadStringCompleted);
client.DownloadStringAsync(url);
}
无效客户端\u DownloadStringCompleted已完成(对象发送方,DownloadStringCompletedEventArgs e)
{
如果(e.Error==null)
{
StringReader流=新StringReader(例如结果);
XmlReader=XmlReader.Create(流);
双Lat=0.00;
双液化天然气=0.00;
字符串CustomerName=“”;
Int32 CustomerId=0;
LocationDataCollection locationList=新位置DataCollection();
while(reader.Read())
{
reader.MoveToContent();
if(reader.NodeType==XmlNodeType.Element)
{
reader.MoveToFirstAttribute();
}
if(reader.NodeType==XmlNodeType.Attribute)
{
if(true==reader.MoveToAttribute(“纬度”))
{
Lat=reader.HasValue?转换为双(reader.Value):0.00;
}
if(true==reader.MoveToAttribute(“经度”))
{
Lng=reader.HasValue?转换为双(reader.Value):0.00;
}
if(true==reader.MoveToAttribute(“CustomerName”))
{
CustomerName=reader.Value.ToString();
}
if(true==reader.MoveToAttribute(“CustID”))
{
CustomerId=Convert.ToInt32(reader.Value);
}
LocationData T=新的LocationData();
T.位置.纬度=纬度;
T.位置.经度=液化天然气;
T.客户名称=客户名称;
位置列表。添加(T);
}
}
IEnumerator ppEnum=locationList.GetEnumerator();
while(ppEnum.MoveNext())
{
this.Add((LocationData)ppEnum.Current);
}
reader.Close();
}
}
}
}
Xaml代码:
<phone:PhoneApplicationPage
x:Class="maps_data_test_2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:t="clr-namespace:maps_data_test_2"
您的问题是,由于您是从可观察集合继承的,而不是将其作为属性,因此无法正确绑定。请为MapItemsControl尝试以下操作:
<m:MapItemsControl x:Name="ListOfItems"
ItemTemplate="{StaticResource LogoTemplate}"
ItemsSource="{Binding}"
DataContext="{StaticResource LocationList}"/>
在运行原始代码时,还应该在调试控制台中获得绑定错误消息。我将使其成为构造函数中初始化的属性,而不是从ObservaleCollection中继承:
public ObservableCollection<LocationData> Locations {get; private set;}
公共可观测集合位置{get;private set;}
@Dan将LocationData collection作为属性,并作为ObservableCollection类型。您可以将数据填充到此集合中 谢谢你的回复,迈克尔。我还在努力了解这里发生了什么。我将把:public observetectionlocations{get;private set;}放在哪里?
<m:MapItemsControl x:Name="ListOfItems"
ItemTemplate="{StaticResource LogoTemplate}"
ItemsSource="{Binding}"
DataContext="{StaticResource LocationList}"/>
public ObservableCollection<LocationData> Locations {get; private set;}