Привязка местоположения канцелярской кнопки в приложении Windows Phone 8 не работает

Я разрабатываю приложение для Windows Phone 8, в котором я должен отображать канцелярскую кнопку в текущем местоположении. при выполнении кода отображается исключение: «Исключение типа« System.NullReferenceException »произошло в MapTestApp.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


person Kuldeep Singh    schedule 12.09.2013    source источник
comment
Вот ссылка на экран исключения, он показывает канцелярскую кнопку как нулевой объект.: docs.google.com/file/d/0By0Y-Dca1cKjYXp4T3ctV1hLUEk/   -  person Kuldeep Singh    schedule 12.09.2013


Ответы (1)


Пожалуйста, попробуйте это прикрепленное свойство

 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}"
person Rakesh R Nair    schedule 12.09.2013