Windows Phone 8应用中的图钉位置绑定不起作用 [英] Pushpin location binding in windows phone 8 app is not working

查看:74
本文介绍了Windows Phone 8应用中的图钉位置绑定不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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代码:

    <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

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

推荐答案

请尝试使用此附加属性

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

这篇关于Windows Phone 8应用中的图钉位置绑定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆