我正在开发一个WP7项目,并尝试以特定方式将图钉绑定到bing地图,但它不会工作。它似乎读取了XML文件,因为它计算了所有项目,然后加载地图,但只是将黑色图钉放在地图的右上角。会不会感到任何想法,为什么它不贬低图钉。非常感谢。 我想我已经包含了所有相关的代码..
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();
}
}
}
}
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"
xmlns:m =“clr- namespace:Microsoft.Phone.Controls.Maps; assembly = Microsoft.Phone.Controls.Maps”
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="LogoTemplate">
<m:Pushpin Location="{Binding Location}" />
</DataTemplate>
<t:LocationDataCollection x:Key="LocationList" IsDataSource="True"/>
<Grid>
<m:Map Height="450" Width="450" x:Name="mMap" Cred entialsProvider=""
Mode="Road"
>
<m:MapItemsControl x:Name="ListOfItems"
ItemTemplate="{StaticResource LogoTemplate}"
ItemsSource="{StaticResource LocationList}">
</m:MapItemsControl>
</m:Map>
</Grid>
</phone:PhoneApplicationPage>
答案 0 :(得分:0)
您的问题是,因为您从可观察集合继承而不是将其作为属性,所以您没有正确绑定。试试这个MapItemsControl:
<m:MapItemsControl x:Name="ListOfItems"
ItemTemplate="{StaticResource LogoTemplate}"
ItemsSource="{Binding}"
DataContext="{StaticResource LocationList}"/>
运行原始代码时,您还应该在调试控制台中收到绑定错误消息。我将使它成为一个在构造函数中初始化的属性,而不是从ObservableCollection&lt;&gt;继承:
public ObservableCollection<LocationData> Locations {get; private set;}
答案 1 :(得分:0)
@Dan将LocationData集合作为属性,并且类型为ObservableCollection。您可以将数据填充到此集合中。