How can I add a Bing Maps Component to my C# Winforms app?
Asked Answered
K

1

2

I have downloaded, installed, and referenced the Bing Maps control in my Winforms app:

enter image description here

...and I added the following using to Form1.cs:

using Microsoft.Maps.MapControl;

...but I still have no Bing Maps control in my Toolbox.

I realize this is a WPF control, and I'm using Winforms, but my understanding is that the WPF control can be used in a Winforms app.

My question is: How can I make the Bing Maps control visible in the Toolbox OR create the Bing Maps control in code?

It probably doesn't matter, but the Runtime version of the map control is 4.0.30319, version = 1.0.0.0

Kerbing answered 25/12, 2020 at 17:7 Comment(2)
Which .NET version:.NET 5.0, .NET Core3.x, .NET Framework 4.X?Apperception
.NET Framework 4.7.2Kerbing
A
5

WPF controls won't be added to Windows Forms toolbox, instead you need to create a WPF UserControl inside your Windows Forms project and add a WPF Map to that, then drop an instance of ElementHost Windows Forms control on your Form and tell the host to show your WPF UserControl.

Example - WPF Bing Maps Control in Windows Forms

  1. Create a Windows Forms Application.

  2. Install Microsoft.Maps.MapControl.WPF NuGet package.

  3. Add a new WPF UserControl to your project:

    Right-click on project → choose Add New Item → Add a new User Control (WPF) (It's located under WPF category)

  4. Add the Map to your WPF user control. Basically you need to add the map assembly to the control by adding the following attribute to your user control:

    xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF"
    

    And adding the map element to the control:

    <Grid>
      <m:Map/>
    </Grid>
    

    Here is the full code for your user control. (Make sure you use the correct namespace):

    <UserControl x:Class="YOURNAMESPACE.UserControl1"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF">
       <Grid>
           <m:Map CredentialsProvider="YOUR MAP KEY" x:Name="myMap" />
       </Grid>
    </UserControl >
    

    • Note: You need a Bing Maps key. If you don't have a Bing maps account, create a Bing Maps account and then get a Bing Maps key from it. For test purpose, you can ignore the map key.

  5. Build your project.

  6. Drop an instance of ElementHost control on your form. You can find it under the WPF Interoperability group in toolbox.

  7. From the smart action panel (at top right of the ElementHost control), set the hosted content to UserControl1 and dock it to the parent container.

  8. Run your application.

There you go, The map will appear in your Windows Forms Application:

enter image description here

More information:

You may want to take a look at the following links for more information:

Apperception answered 27/12, 2020 at 10:42 Comment(4)
Great answer, Reza! The only thing I needed to look up was more specifics on step 3, which involved "Add New Item > WPF User Control"Kerbing
Thanks for the feedback, I edited the step to make it a bit clearer.Apperception
It's worth noting that the WPF map control is old and no longer being updated. I wouldn't recommend using it for any new development. The UWP map control can be hosted in Winforms applications using XAML Islands - see learn.microsoft.com/en-us/windows/apps/desktop/modernize/…Reify
@DuncanLawler that's true, it's old; however, one of the reasons that someone may still wants to use WPF Bing Maps, is because it's supported on all windows versions; but UWP map, is only available on Windows 10. I've posted an answer here for those who are looking to UWP Map as an option :)Apperception

© 2022 - 2024 — McMap. All rights reserved.