2

我想通过设置不透明度来淡化航空地图图层。但是,我不想为在地图顶部呈现的任何对象(即图钉)保留任何 alpha(无不透明度)。

任何人都知道这是否可行(API 是否以某种方式支持这一点)?

谢谢!

4

1 回答 1

1

需要一种方法来MapTileLayer设置 Map 控件用于显示地图本身的内部样式。不幸的是,API 不提供该级别的访问权限。

但是,我们可以使用 VisualTreeHelper 来访问MapTileLayer. 我使用这个博客中的扩展类来帮助解决这个问题。有了这个项目中的课程,我们可以做一些像这样的笨拙的事情:-

        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>

图钉将保持完全不透明,但地图图像本身将淡出。

于 2011-05-07T13:46:50.803 回答