绑定Windows Phone 8地图API扩展

4

我正在尝试使用数据绑定与 Windows Phone Toolkit 的地图 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;

Thanks in advance for your help,

Best regards


我有同样的问题。无法仅通过xaml文件使绑定工作。 - likebobby
1个回答

4

MapItemsControl继承自DependencyObject,而不是FrameworkElement,因此DataContext不会传播。长话短说……除非你有某种设置Binding的Source属性的方式,否则你不能从XAML数据绑定MapItemsControl。

如果手机上的RelativeSource的FindAncestor模式有效,可能可以解决这个问题,但显然并不行。这使我们要么在代码中创建绑定,要么(更现实地)在代码中设置ItemsSource。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接