2013-08-29 47 views
0

我正尝试在WinRT应用程序中为Bing地图创建自定义图钉。我的问题是,我需要从我的页面上的实际地图的引用,以便在我的userControl中正确地固定图标。因此,例如,这是我的DataTemplate绑定到地图,并正常工作的图钉。为了让我的自定义userControl正确定位,我需要对userControl中父映射的引用。WinRT UserControl的参数化构造函数

这是我的XAML:

<m:MapItemsControl x:Name="Pushpinss" ItemsSource="{Binding InventoryItems}"> 
     <m:MapItemsControl.ItemTemplate> 
      <DataTemplate> 
      <!-- NORMAL PUSHPIN WORKS --> 
      <m:Pushpin> 
       <m:MapLayer.Position> 
       <m:Location Latitude="{Binding WarehouseLatitude}" 
          Longitude="{Binding WarehouseLongitude}" /> 
       </m:MapLayer.Position> 
      </m:Pushpin> 
      <!-- CUSTOM CONTROL DISPLAYS BUT DOES NOT POSITION CORRECTLY BECAUSE I NEED A REFERENCE TO THE MAP--> 
      <View:GPSIcon Latitude="{Binding WarehouseLatitude}" 
          Longitude="{Binding WarehouseLongitude}" 
          Radius="100000"/> 
       <x:Arguments> 
       </x:Arguments> 
      </DataTemplate> 
     </m:MapItemsControl.ItemTemplate> 
     </m:MapItemsControl> 

这是我的自定义控制:

public sealed partial class GPSIcon : UserControl 
    { 
    private Map _map; 
    private const double EARTH_RADIUS_METERS = 6378137; 

    public GPSIcon(Map map) 
    { 
     this.InitializeComponent(); 

     _map = map; 
     _map.ViewChanged += (s, e) => 
     { 
     UpdateAccuracyCircle(); 
     }; 
    } 

    public static readonly DependencyProperty LatitudeProperty = 
      DependencyProperty.Register("Latitude", typeof(double), typeof(GPSIcon), new PropertyMetadata(0)); 

    public static readonly DependencyProperty LongitudeProperty = 
     DependencyProperty.Register("Longitude", typeof(double), typeof(GPSIcon), new PropertyMetadata(0)); 

    public static readonly DependencyProperty RadiusProperty = 
     DependencyProperty.Register("Radius", typeof(double), typeof(GPSIcon), new PropertyMetadata(0)); 

    public double Latitude 
    { 
     get { return (double)GetValue(LatitudeProperty); } 
     set { SetValue(LatitudeProperty, value); } 
    } 

    public double Longitude 
    { 
     get { return (double)GetValue(LongitudeProperty); } 
     set { SetValue(LongitudeProperty, value); } 
    } 

    /// <summary> 
    /// Radius in Metres 
    /// </summary> 
    public double Radius 
    { 
     get { return (double)GetValue(RadiusProperty); } 
     set 
     { 
     SetValue(RadiusProperty, value); 
     UpdateAccuracyCircle(); 
     } 
    } 

    private void UpdateAccuracyCircle() 
    { 
     if (_map != null && Radius >= 0) 
     { 
     double groundResolution = Math.Cos(_map.Center.Latitude * Math.PI/180) * 2 * Math.PI * EARTH_RADIUS_METERS/(256 * Math.Pow(2, _map.ZoomLevel)); 
     double pixelRadius = Radius/groundResolution; 

     AccuracyCircle.Width = pixelRadius; 
     AccuracyCircle.Height = pixelRadius; 
     AccuracyCircle.Margin = new Thickness(-pixelRadius/2, -pixelRadius/2, 0, 0); 
     } 
    } 
    } 

这是可能的呢?我已经使用x也尝试:参数指令如下所述: http://msdn.microsoft.com/en-us/library/ee795382.aspx

感谢

回答

0

更新1

做如下改变

1)添加空的构造。

public GPSIcon() 
{ 
    this.InitializeComponent(); 
} 

2)声明类型的DP Map

public Map MyMap 
{ 
    get { return (Map)GetValue(MyMapProperty); } 
    set { SetValue(MyMapProperty, value); } 
} 

public static readonly DependencyProperty MyMapProperty = 
    DependencyProperty.Register("MyMap", typeof(Map), typeof(GPSIcon), new PropertyMetadata(default(Map), OnMapSet)); 

private static void OnMapSet(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
    _map = ((GPSIcon)(d)).MyMap; 
    _map.ViewChanged += (ss, ee) => 
    { 
     ((GPSIcon)(d)).UpdateAccuracyCircle(); 
    }; 
} 

3)通Map对象像这样在XAML

<m:Map x:Name="objMap"> 
    <m:MapItemsControl x:Name="Pushpinss" ItemsSource="{Binding InventoryItems}"> 
     <m:MapItemsControl.ItemTemplate> 
      <DataTemplate> 
       <View:GPSIcon Latitude="{Binding WarehouseLatitude}" 
           Longitude="{Binding WarehouseLongitude}" 
           Radius="100000" 
           MyMap="{Binding ElementName=objMap}"/> 
      </DataTemplate> 
     </m:MapItemsControl.ItemTemplate> 
    </m:MapItemsControl> 
</m:Map> 

声明Map类型的一个更依赖属性,然后您应该将01当前地图实例作为该DP的值

简单地说,你需要遵循同样的逻辑如何Pass parameter to constructor from xaml in Silverlight

+0

谢谢,我已经试过了,但我不能让它绑定。我应该为WinRT使用什么绑定语法?可视化树是 jqIndy

+0

请参阅更新1. – Xyroid

+0

非常感谢。我不能等待Win8.1发布。调试是一场噩梦。我不得不重新创建这个应用程序,因为我的自定义控件中没有任何工作了,仍然没有出于某种原因。你的例子很有意义。我会让你知道,如果它的工作,当我得到它建立:) – jqIndy

0

为了让您的自定义的UIElement正确定位在地图上,你可以做的,而不是在代码中这样做是什么简单地设置的位置UIElement与设置图钉位置的方式相同。

例如:

<View:GPSIcon Radius="100000"> 
    <m:MapLayer.Position> 
     <m:Location Latitude="{Binding WarehouseLatitude}" 
        Longitude="{Binding WarehouseLongitude}" /> 
    </m:MapLayer.Position> 
</View:GPSIcon> 
+0

这太好了。非常感谢这个评论 – jqIndy