简体   繁体   中英

MapControl using XAML Island in WPF: default map layer is shown over custom tile layer

I've added a MapControl using this instruction to my WPF application. I wanted to add custom map layer to it, so I've added a OpenStreetMap tile layer using this instruction . I want to remove the default map at all, but it doesn't work.

I've tested the original MapControl in a UWP app and it works.

My code in WPF:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:controls="clr-namespace:Microsoft.Toolkit.Wpf.UI.Controls;assembly=Microsoft.Toolkit.Wpf.UI.Controls"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <controls:MapControl x:Name="mapControl" />
    </Grid>
</Window>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            HttpMapTileDataSource dataSource = new HttpMapTileDataSource("http://c.tile.openstreetmap.org/{zoomlevel}/{x}/{y}.png");

            MapTileSource tileSource = new MapTileSource(dataSource);
            tileSource.Visible = true;
            tileSource.Layer = MapTileLayer.BackgroundReplacement;
            tileSource.IsFadingEnabled = false;
            mapControl.Style = Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT.MapStyle.None;
            mapControl.TileSources.Add(tileSource);
        }
    }

This is the WPF application. It shows OpenStreetMap layer and also default Bing map layer on top:

WPF 中的地图控件

This is the UWP app and shows only OpenStreetMap perfectly:

UWP 中的 MapControl

Is there any solution to fix it in WPF?

Update

I find out that this line doesn't apply correctly and it caused the error:

mapControl.Style = Microsoft.Toolkit.Win32.UI.Controls.Interop.WinRT.MapStyle.None;

Finally I found the solution. Add these lines to the initialization code:

Windows.UI.Xaml.Controls.Maps.MapControl originalMapControl =
    mapControl.GetUwpInternalObject() as Windows.UI.Xaml.Controls.Maps.MapControl;

if (originalMapControl != null)
{
    originalMapControl.Style = MapStyle.None;
}

It fixes the defect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM