数据绑定 – 与Windows Phone 8的Map API扩展绑定

我正在尝试使用 windows phone toolkit的map api扩展名进行数据绑定.我正在做 :

<maps:Map x:Name="Map" Center="47.6,-122.3" ZoomLevel="12">
    <maptk:MapExtensions.Children>
        <maptk:MapItemsControl ItemsSource="{Binding PositionList}">
            <maptk:MapItemsControl.ItemTemplate>
                <DataTemplate>
                    <maptk:pushpin GeoCoordinate="{Binding}" />
                </DataTemplate>
            </maptk:MapItemsControl.ItemTemplate>
        </maptk:MapItemsControl>
    </maptk:MapExtensions.Children>
</maps:Map>

我的代码背后:

public partial class MainPage : PhoneApplicationPage,INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this,new PropertyChangedEventArgs(name));
    }

    private bool NotifyPropertyChanged<T>(ref T variable,T valeur,[CallerMemberName] string name= null)
    {
        if (object.Equals(variable,valeur)) return false;

        variable = valeur;
        NotifyPropertyChanged(name);
        return true;
    }

    private IEnumerable<GeoCoordinate> positionList;
    public IEnumerable<GeoCoordinate> PositionList
    {
        get { return positionList; }
        set { NotifyPropertyChanged(ref positionList,value); }
    }

    public MainPage()
    {
        InitializeComponent();
        PositionList = new List<GeoCoordinate> 
        { 
             new GeoCoordinate(47.6050338745117,-122.334243774414),new GeoCoordinate(47.6045697927475,-122.329885661602),new GeoCoordinate(47.605712890625,-122.330268859863),new GeoCoordinate(47.6015319824219,-122.335113525391),new GeoCoordinate(47.6056594848633,-122.334243774414)
        };

        DataContext = this;
    }
}

但我在地图上看不到任何图钉:(

我究竟做错了什么 ?

请注意,如果我在代码隐藏文件中使用它,它正在工作

MapExtensions.GetChildren(Map).OfType<MapItemsControl>().First().ItemsSource = PositionList;

在此先感谢您的帮助,

最好的祝福

解决方法

MapItemsControl派生自DependencyObject,而不是FrameworkElement,因此DataContext不会传播.长篇故事…你不能从XAML绑定MapItemsControl的数据,除非你有办法设置Binding的Source属性.

如果RelativeSource的FindAncestor模式在手机上运行,​​可能可以解决这个问题,但显然没有.这使我们无法在代码中创建绑定,或者(更现实地)在代码中设置ItemsSource.

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...