Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...