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

1.9k views Asked by At

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:

MapControl in WPF

This is the UWP app and shows only OpenStreetMap perfectly:

MapControl in UWP

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;
1

There are 1 answers

0
Mahdi Ataollahi On BEST ANSWER

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.