This example shows how you can use WPF Bing Maps Control in a Windows Forms application, enable animation and set the initial view of the map. To do so, follow these instructions:
-
Create a Windows Forms Application.
-
Install Microsoft.Maps.MapControl.WPF NuGet package.
-
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)
-
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.
-
Build your project.
-
Drop an instance of ElementHost control on your form. You can find it under the WPF Interoperability group in toolbox.
-
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. -
Handle the Load event of the
Form
and use the following code:private void Form1_Load(object sender, EventArgs e) { this.userControl11.myMap.AnimationLevel = AnimationLevel.Full; this.userControl11.myMap.Loaded += MyMap_Loaded; } private void MyMap_Loaded(object sender, System.Windows.RoutedEventArgs e) { var location = new Location(47.604, -122.329); this.userControl11.myMap.SetView(location, 12); }
Make sure you use
using Microsoft.Maps.MapControl.WPF;
. -
Run your application.
There you go, The map will appear in your Windows Forms Application and the map zooms in Seattle as center location:
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
- How can I add a Bing Maps Component to my C# Winforms app?
- Bing Maps WPF Control
- Developing with the Bing Maps WPF Control
- Bing Maps WPF Control API Reference