0

我似乎无法理解这个图钉绑定,我需要更多帮助。

我有以下代码从 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>
4

1 回答 1

1

从您发布的代码来看,问题似乎是 MapsItemsControl 的 ItemsSource 没有数据绑定到集合。它绑定到一个类型。

好的,除非您定义 DataContext 等,否则数据绑定并不能真正起作用。您在这里混合和匹配范例。我认为在某个时候学习 MVVM 和数据绑定会很好,但是现在,我认为只做一个快速而肮脏的方法是可以的。

简单的方法是分配 ItemSource。

为此,首先命名您的 MapsItemControl,以便您可以在代码隐藏中访问它。

<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 x:Name="RhysMapItems" ItemsSource="{Binding TransitVariables}" Height="494">
    <my:MapItemsControl.ItemTemplate>
        <DataTemplate>
            <my:Pushpin Location="{Binding Location}"  />
        </DataTemplate>
    </my:MapItemsControl.ItemTemplate>
</my:MapItemsControl>

在您的下载字符串完成处理程序中,您应该能够做到这一点:

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

       // This should bind the itemsource properly
       // Should use Dispatcher actually...see below
       RhysMapItems.ItemsSource = Transitresults.ToList();
    }

现在,这里需要注意的是,您的 DownloadStringCompleted 处理程序很可能会在与 UI 线程不同的线程上被调用。

在这种情况下,您需要使用 Dispatcher.BeginInvoke() 来修改 ItemSource 属性。

this.RootVisual.Dispatcher.BeginInvoke( _=> { RhysMapItems.ItemsSource = Transitresults.ToList();});
于 2011-09-21T23:08:14.583 回答