结合图钉Bing地图(Binding pushpins to Bing maps)

2019-09-29 00:02发布

进出口工作机管局WP7项目,并尝试冰图钉冰一种特殊的方式映射,但它只是不会工作。 这似乎读取XML文件就OK了,因为它计算所有的项目,然后加载地图,而只是把黑色的图钉在地图的右上角。 将是为什么它不dispalying图钉任何想法感谢。 非常感谢。 我想我已经包括了所有培训相关代码..

   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-名称空间:Microsoft.Phone.Controls.Maps;装配= 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>

Answer 1:

你的问题是,因为你是从观察集合继承,而不是使其成为一个属性,你是不是正确结合。 试试这个为MapItemsControl:

<m:MapItemsControl x:Name="ListOfItems" 
    ItemTemplate="{StaticResource LogoTemplate}"
    ItemsSource="{Binding}"
    DataContext="{StaticResource LocationList}"/> 

你也应该在你的调试控制台运行你原来的代码时得到一个绑定错误消息。 而不是从的ObservableCollection <> inherinting的,我会使其成为获取构造函数初始化的属性:

public ObservableCollection<LocationData> Locations {get; private set;}


Answer 2:

@丹有LocationData集合作为一个属性,类型的ObservableCollection的。 你可以填充数据插入此集合。



文章来源: Binding pushpins to Bing maps