Binding pushpins to Bing maps

2019-08-11 09:57发布

问题:

Im working a a WP7 project, and try to bing pushpins to bing maps a particular way, but it just wont work. It seems to read the XML file ok as it counts all the items, and then loads the map, but just puts the black pushpin in the top right hand corner of the map. Would be grateful for any ideas why its not dispalying the pushpins. Many thanks. I think I have included all the relevent code..

   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 code:

<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>

回答1:

Your problem is that because you are inheriting from the observable collection rather than making it a property, you are not binding correctly. Try this for the MapItemsControl:

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

You should also get a binding error message in your debug console when running your original code. Instead of inherinting from ObservableCollection<>, I would make it a property that gets initialized in the constructor:

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


回答2:

@Dan Have the LocationData collection as a property, and as of type ObservableCollection. And you can populate the data into this collection.