Overview
This walkthrough shows how to create a new Bing Maps Silverlight Control project with either Visual Studio 2008 SP1 or Blend 3. While some of this material has been covered on other sites and blogs, I’m writing about it here for two reasons:
- Some of the other posts are now outdated, using older, beta or CTP versions of the APIs and development tools.
- In future posts about developing with the Bing Maps Silverlight Control, I want to be able to refer to this “Getting Started” guide so I don’t have to keep repeating the basics.
Download and Install the Development Tools
If you haven’t already done so, download and install the following applications, SDKs and Toolkits:
Required
Optional
[more]
Setup a Big Maps Developer Account Using Your Windows Live ID
If you don’t already have a Windows Live ID, you’ll need to signup for one on the Windows Live site: https://signup.live.com/signup.aspx
One you have your Windows Live ID, go the the Bing Maps Developer Portal: https://www.bingmapsportal.com/
Click on the “Create or view Bing Maps keys” link in the left menu:
When prompted, login with your Windows Live ID. After logging in, you will be redirected to the proper page.
Enter the Application Name and the URL, and click the Create Key button.
Here you can see I’ve created two keys. One for development and testing through the http://localhost url and one for running applications on a live URL (in this case, my blog.) I’ve blacked out the actual keys.
Create a New Bing Maps Silverlight Application with Visual Studio
When creating a new Silverlight application, you can start with either Visual Studio or Blend. First we’ll look at how to create the application with Visual Studio. Then in the next section we’ll see how it’s done with Blend.
Start Visual Studio 2008 and choose File > New > Project…
Select Silverlight from the Project types tree, and Silverlight Application from the Templates list.
Give your application a name and click OK.
A dialog will appear asking if you want to host the application in a new web site. Make sure this option is checked, select ASP.NET Web Application Project, and click OK.
After the project is created, add references to the Bing Maps assemblies.
Right click on the References folder in the BingMapsApp project and choose Add Reference…
Select the Browse tab and browse to the Libraries directory in the Bing Maps Silverlight Control installation folder. The default installation location is C:\Program Files\Bing Maps Silverlight Control\V1\Libraries.
Select both the Microsoft.Maps.MapControl.dll and Microsoft.Maps.MapControl.Common.dll assemblies, and click OK.
Modify the xaml for the MainPage.xaml user control by adding an xml namespace declaration for the Bing Maps assembly.
xmlns:bing="clr-namespace:Microsoft.Maps.MapControl;assembly=Microsoft.Maps.MapControl"
Note: The samples on Microsoft’s Bing Maps Silverlight Control Interactive SDK all use the “m” prefix for the namespace, but I prefer to use “bing” as it makes the code a little more expressive. Hat tip to Bobby Diaz’s Bing Maps Silverlight MVVM Sample for that idea.
Add a <bing:Map …/> element inside the layout root and set the CredentialsProvider property to the key that you generated earlier.
At this point, you should be able to press F5 and see the default map in your browser.
If you see the “Invalid Credentials” message then double check to make sure you have properly set the CredentialsProvider property.
At this point I like to clean up the Web project a little bit.
Open up the BingMapsAppTestPage.aspx page, select everything from <!DOCTYPE … down to the bottom of the file, and Ctrl+C to copy it to the clipboard.
Open up the Default.aspx page, delete everything from <!DOCTYPE … down to the bottom of the file, and paste in the contents from the clipboard.
Save the Default.aspx file.
Delete the BingMapsAppTestPage.aspx page and the BingMapsAppTestPage.html file.
Right click on Default.aspx and choose Set As Start Page…
Press F5 to run the project and confirm that the control is now hosted in the default page.
Create a New Bing Maps Silverlight Application with Blend
Now that we’ve covered how to create a new Bing Maps Silverlight application with Visual Studio 2008, lets take a look at how to accomplish the same task with Blend 3.
Start Blend 3 and choose File > New Project…
Select Silverlight from the Project types tree, and Silverlight 3 Application + Website from the template list.
Give your application a name, and click OK.
When you create a new Silverlight project in Blend, the structure of the web site is different than if you had created it in Visual Studio. The Silverlight app is hosted in an html page rather than an ASP.NET aspx page.
After the project is created, add references to the Bing Maps assemblies.
Right click on the References folder in the BingMapsApp project and choose Add Reference…
Browse to the Libraries directory in the Bing Maps Silverlight Control installation folder. The default installation location is C:\Program Files\Bing Maps Silverlight Control\V1\Libraries.
Select both the Microsoft.Maps.MapControl.dll and Microsoft.Maps.MapControl.Common.dll assemblies, and click OK.
Click the Asset Library button and type “map” in the search box to filter the assets to those controls that contain the phrase “map” in their name. Then select the Bing Maps Silverlight Control from the available choices.
Note: The preceding screen shot shows two controls named “Map”. The other one is ESRI’s ArcGIS API for Microsoft Silverlight/WPF Map Control. If you also happen to have this library installed, make sure you choose the correct Map.
After selecting the Map control, it will appear below the Asset Library button.
Double click the Map control button to add a map to the artboard at the default size.
Click on the Xaml button on the upper right hand side of the artboard to switch to Xaml view.
Notice that when you added the Map control Blend automatically added a namespace declaration for you.
Since the automatically generated namespace name is rather verbose, change it to “bing”.
Click the Design button on the upper right hand side of the code window to switch back to the Design view.
In the Properties window Layout bucket, the right and bottom margins will have defaulted to 240 and 180 respectively.
Set each of these properties to 0 so that the map fills the whole layout.
At the bottom of the Properties window, click arrow next to the Miscellaneous tab to expand it.
Paste your API Key into the text box for the CredentialsProvider property and hit return.
At this point you can click F5 to compile and run the project. Notice that the map does not fill the entire browser window like it did with the Visual Studio solution we built in in part 1.
This is because Blend sets an explicit Width and Height by default when creating a User Control as part of a new project, while Visual Studio sets DesignWidth and DesignHeight properties with the “Ignorable” namespace prefix.
Switch back to Xaml view, and replace the names of the Width and Height properties with d:DesignWidth and d:DesignHeight.
Now run the project again and the Map will fill the whole browser window.
As was the case in the Visual Studio walkthrough, if you see the “Invalid Credentials” message then double check to make sure you have properly set the CredentialsProvider property.
Wrapup
This post presented two methods for creating a new Bing Maps Silverlight Control project. The first method gave a walkthrough using Visual Studio 2008 SP1, and the second showed how to accomplish the same task with Blend 3. You can use either method, depending on which tool you are most comfortable with.
Additional Resources
Microsoft
Bing Maps MVVM Samples