2011-05-07 58 views

回答

1

需要的是一種設計Map控件用來顯示地圖本身的內部樣式MapTileLayer的方法。不幸的是,API不提供這種訪問級別。

但是,我們可以使用VisualTreeHelper訪問MapTileLayer。我使用blog中的擴展類來幫助解決這個問題。有了這個類項目中存在的,我們可以做一些cludgy這樣的: -

 MapTileLayer tileLayer = myMapControl.Descendents().OfType<MapTileLayer>().FirstOrDefault(); 
     if (tileLayer != null) 
     { 
      tileLayer.Opacity = 0.5; // set the opacity desired. 
     } 

但是它可能最好通過創建從Map派生的新類,並像分配的風格,而不是一個單一的財產做正確Opacity

[StyleTypedProperty(Property = "TileLayerStyle", StyleTargetType = typeof(MapTileLayer))] 
public class MapEx : Map 
{ 
    #region public Style TileLayerStyle 
    public Style TileLayerStyle 
    { 
     get { return GetValue(TileLayerStyleProperty) as Style; } 
     set { SetValue(TileLayerStyleProperty, value); } 
    } 

    public static readonly DependencyProperty TileLayerStyleProperty = 
     DependencyProperty.Register(
      "TileLayerStyle", 
      typeof(Style), 
      typeof(MapEx), 
      new PropertyMetadata(null, OnTileLayerStylePropertyChanged)); 

    private static void OnTileLayerStylePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     MapEx source = d as MapEx; 
     source.SetTileLayerStyle(); 
    } 
    #endregion public Style TileLayerStyle 

    public override void OnApplyTemplate() 
    { 
     base.OnApplyTemplate(); 
     SetTileLayerStyle(); 
    } 

    private void SetTileLayerStyle() 
    { 
     MapTileLayer tileLayer = this.Descendents().OfType<MapTileLayer>().FirstOrDefault(); 
     if (tileLayer != null) 
     { 
      tileLayer.Style = TileLayerStyle; 
     } 
    } 

有了這個衍生的地方,我們可以這樣做: -

<UserControl x:Class="HostBingMaps.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:HostBingMaps" 
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480" 
    xmlns:m="clr-namespace:Microsoft.Maps.MapControl;assembly=Microsoft.Maps.MapControl"> 
    <Grid x:Name="LayoutRoot"> 
     <Grid.Resources> 
      <Style x:Key="TileLayerStyle" TargetType="m:MapTileLayer"> 
       <Setter Property="Opacity" Value="0.2" /> 
      </Style> 
     </Grid.Resources> 
     <local:MapEx Mode="Aerial" TileLayerStyle="{StaticResource TileLayerStyle}" AnimationLevel="UserInput" UseInertia="True" CredentialsProvider="__creds_here__"> 
       <!-- Pushpins here --> 
     </local:MapEx> 
    </Grid> 
</UserControl> 

圖釘將保持完全不透明的,但地圖圖像本身將被淡化了。

+0

甜!下週我會檢查一下,當我有機會玩這個擴展。但告訴我一件事。什麼是VisualTreeHelper? – AlvinfromDiaspar 2011-05-08 06:44:23

+0

@AlvinfromDiaspar:請參閱http://msdn.microsoft.com/en-us/library/system.windows.media.visualtreehelper%28VS.95%29.aspx – AnthonyWJones 2011-05-08 07:59:41

+0

很酷。謝謝。但是這個可視化助手看起來並沒有什麼幫助。我已經編寫了遞歸可視化樹爬蟲,看起來比這更強大。我錯過了對此有用的東西嗎? – AlvinfromDiaspar 2011-05-11 04:48:46