Pushpin location binding in windows phone 8 app is not working

1.2k views Asked by At

i am developing a windows phone 8 app, in which i have to display a pushpin to current location. while running the code it is showing an exception: "An exception of type 'System.NullReferenceException' occurred in MapTestApp.DLL but was not handled in user code"

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 code:

    <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>

Here is the link to the exception screen, it shows pushpin as null object: https://docs.google.com/file/d/0By0Y-Dca1cKjYXp4T3ctV1hLUEk/edit?usp=sharing

1

There are 1 answers

0
Rakesh R Nair On

Please try this attached property

 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}"