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

两盒软妹~` 提交于 2021-02-13 17:26:23

问题


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

...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


回答1:


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:

More information:

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

  • WPF and Windows Forms Interoperation
  • Walkthrough: Hosting a WPF Composite Control in Windows Forms
  • Create a WPF Application that uses Bing Maps Control


来源:https://stackoverflow.com/questions/65450109/how-can-i-add-a-bing-maps-component-to-my-c-sharp-winforms-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!