我似乎无法理解这个图钉绑定,我需要更多帮助。
我有以下代码从 XML 解析并在 Lat、Lon 和 Alt 中拆分坐标字符串。我想做的是将这些点显示为我的 bing 地图上的图钉。
我认为通过在其中创建一个新的地理坐标对象,Location
我可以将其绑定到我的图钉位置,但没有显示任何内容。我哪里错了?
namespace Pushpins_Itemsource
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
WebClient busStops = new WebClient();
busStops.DownloadStringCompleted += new DownloadStringCompletedEventHandler(busStops_DownloadStringCompleted);
busStops.DownloadStringAsync(new Uri("http://www.domain/source.xml"));
}
void busStops_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
return;
var busStopInfo = XDocument.Load("Content/BusStops2.xml");
var Transitresults = from root in busStopInfo.Descendants("Placemark")
let StoplocationE1 = root.Element("Point").Element("coordinates")
let nameE1 = root.Element("name")
select new TransitVariables
(StoplocationE1 == null ? null : StoplocationE1.Value,
nameE1 == null ? null : nameE1.Value);
}
// Add properties to your class
public class TransitVariables
{
// Add a constructor:
public TransitVariables(string stopLocation, string name)
{
this.StopLocation = stopLocation;
this.Name = name;
if (!string.IsNullOrEmpty(StopLocation))
{
var items = stopLocation.Split(',');
this.Lon = double.Parse(items[0]);
this.Lat = double.Parse(items[1]);
this.Alt = double.Parse(items[2]);
}
}
public string StopLocation { get; set; }
public string Name { get; set; }
public double Lat { get; set; }
public double Lon { get; set; }
public double Alt { get; set; }
}
public class TransitViewModel
{
ObservableCollection<TransitVariables> Transitresults ;
public ObservableCollection<TransitVariables> TransitCollection
{
get { return Transitresults; }
}
}
}
}
XAML 看起来像这样。
<my:Map ZoomLevel="6" Height="500" HorizontalAlignment="Left" Margin="0,6,0,0" CopyrightVisibility="Collapsed" LogoVisibility="Collapsed" Name="Map" VerticalAlignment="Top" Width="456">
<my:MapItemsControl ItemsSource="{Binding TransitVariables}" Height="494">
<my:MapItemsControl.ItemTemplate>
<DataTemplate>
<my:Pushpin Location="{Binding Location}" />
</DataTemplate>
</my:MapItemsControl.ItemTemplate>
</my:MapItemsControl>
</my:Map>