2013-08-29 138 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