0

WinRT アプリケーションで Bing Maps のカスタム プッシュピンを作成しようとしています。私の問題は、userControl にアイコンを正しく固定するために、ページから実際のマップへの参照が必要なことです。たとえば、これはマップにバインドされ、通常の画鋲に対して正常に機能する私の DataTemplate です。カスタム userControl を正しく配置するには、userControl 内の親 Map への参照が必要です。

これは私の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:Arguments ディレクティブも使用してみました: http://msdn.microsoft.com/en-us/library/ee795382.aspx

ありがとう

4

2 に答える 2

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();
    };
}

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

タイプの依存関係プロパティをもう 1 つ宣言Mapしてから、現在のマップ インスタンスをその DP の値として渡す必要があります。<View:GPSIcon ... />

簡単に言うと、Silverlight で xaml からコンストラクターにパラメーターを渡す方法と同じロジックに従う必要があります。

于 2013-08-29T10:58:13.990 に答える
0

カスタム UIElement をマップ上で適切に配置するには、コードでこれを行う代わりに、画鋲の位置を設定するのと同じ方法で UIElement の位置を設定するだけです。

例えば:

<View:GPSIcon Radius="100000">
    <m:MapLayer.Position>
       <m:Location Latitude="{Binding WarehouseLatitude}"
                   Longitude="{Binding WarehouseLongitude}" />
    </m:MapLayer.Position>
</View:GPSIcon>
于 2013-08-29T12:58:20.593 に答える