Windows Phone 8 应用程序中的图钉位置绑定不起作用



我正在开发一个Windows Phone 8应用程序,其中我必须显示当前位置的图钉。运行代码时,它显示异常:"MapTestApp 中发生了类型为'System.NullReferenceException'的异常.DLL但未在用户代码中处理"

public MainPage()
{
    InitializeComponent();
        geolocator = new Geolocator();
        geolocator.DesiredAccuracy = PositionAccuracy.High;
        geolocator.MovementThreshold = 100; // The units are meters.
        geolocator.StatusChanged += geolocator_StatusChanged;
        geolocator.PositionChanged += geolocator_PositionChanged;
}
void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
    Dispatcher.BeginInvoke(() =>
    {
        pushpin1.DataContext = args.Position.Coordinate;
    });
}

.xaml 代码:

    <maps:Map x:Name="MyMap" Center="{Binding}" ZoomLevel="15">
        <toolkit:MapExtensions.Children>
            <toolkit:Pushpin x:Name="pushpin1" GeoCoordinate="{Binding}">
                <toolkit:Pushpin.Template>
                    <ControlTemplate TargetType="toolkit:Pushpin">
                        <StackPanel>
                            <ContentPresenter x:Name="content" Content="{TemplateBinding Content}" HorizontalAlignment="Left"></ContentPresenter>
                            <Path Data="M0,0L1,1L2,0L2,0L1,0L0,0Z"
                              Fill="#00AAFF"
                              Stretch="Fill"
                              Margin="-2,0"
                              Height="120"
                              Width="30"
                              Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Visibility, Mode=TwoWay}"
                              HorizontalAlignment="Left"
                              />
                        </StackPanel>
                    </ControlTemplate>
                </toolkit:Pushpin.Template>
            </toolkit:Pushpin>
        </toolkit:MapExtensions.Children>
    </maps:Map>

这是异常屏幕的链接,它将图钉显示为空对象:https://docs.google.com/file/d/0By0Y-Dca1cKjYXp4T3ctV1hLUEk/edit?usp=sharing

请尝试此附加属性

 public static class PushPinExtension
{
    public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.RegisterAttached("ItemsSource",
                                                                     typeof(IEnumerable), typeof(PushPinExtension),
                                                                     new PropertyMetadata(OnPushPinPropertyChanged));
    private static void OnPushPinPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var uie = (UIElement)d;
        var pushpin = MapExtensions.GetChildren((Map)uie).OfType<MapItemsControl>().FirstOrDefault();
        if (pushpin != null) pushpin.ItemsSource = (IEnumerable)e.NewValue;
    }
    public static IEnumerable GetItemsSource(DependencyObject obj)
    {
        return (IEnumerable)obj.GetValue(ItemsSourceProperty);
    }
    public static void SetItemsSource(DependencyObject obj, IEnumerable value)
    {
        obj.SetValue(ItemsSourceProperty, value);
    }
}
 dp:PushPinExtension.ItemsSource="{Binding Path=PushpinCollection}"

相关内容

  • 没有找到相关文章

最新更新