0

この画鋲のバインディングに頭を悩ませているようには見えないので、もう少し助けが必要です。

XMLから解析し、Lat、Lon、Altで座標文字列を分割する次のコードがあります。私がやりたいのは、これらのポイントをBingMapに画鋲として表示することです。

でGeocoordinatesの新しいオブジェクトを作成することで、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に名前を付けて、codebheindでアクセスできるようにします。

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

ここでの1つの注意点は、DownloadStringCompletedハンドラーがUIスレッドとは異なるスレッドで呼び出される可能性が非常に高いことです。

この場合、Dispatcher.BeginInvoke()を使用してItemSourceプロパティを変更する必要があります。

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