Creating a Basic Application Using the Silverlight Map Control

This documentation is no longer available on MSDN, however it is available as a CHM download.

This topic describes how to create a basic application that uses the Bing Maps Silverlight Control.

Note

If you do not need the full functionality of the Bing Maps Silverlight Control and just want to display a simple Silverlight map on your Web page, you can use the embedded map feature of the map control. This is described in the Embedding a Map into Your Web Page topic.

If you are interested in using JavaScript to access the Bing Maps Silverlight Control functionality, see the Accessing the Control Using Script topic.

Create a Silverlight Project

To begin, create a Silverlight project in Visual Studio.

  1. Open Visual Studio 2008.

  2. Select File the main menu.

  3. Select New, and then Project from the menu.

  4. In the New Project dialog box, under the language of your choice (for example, Visual C#), select Silverlight.

  5. Select a Silverlight Application from the available templates and then click OK.

    Ee681903.bc1bd218-cdcf-4f17-a96f-18cc88a0f73d(en-us,MSDN.10).jpg

  6. When asked, make sure the Host the Silverlight application in a new Web site option is checked. Because of URL access restrictions in Silverlight, your Web page must be hosted using the HTTP scheme for it to be able to access Bing Maps tiles. For more information about Silverlight URL access restrictions, see https://msdn.microsoft.com/en-us/library/cc189008(VS.95).aspx.

    Ee681903.b4a7940e-b2e1-494e-a457-6410bf4e01ce(en-us,MSDN.10).jpg

Display a Map

Next, reference the map control dlls in your project and display the default map in your application.

  1. With the Silverlight project selected (not the Web project), select Project then Add Reference from the Visual Studio main menu.

  2. In the Add Reference dialog box, click the Browse tab.

  3. Browse to the location of your Bing Maps Silverlight Control installation. Open the Libraries folder, select the Microsoft.Maps.MapControl.dll and the Microsoft.Maps.MapControl.Common.dll files and click OK.

  4. Add the map control assembly to MainPage.xaml of your Silverlight project by creating a namespace in your UserControl. The MainPage.xaml now looks like this:

    <UserControl x:Class="SilverlightTestApplication.MainPage"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:m="clr-namespace:Microsoft.Maps.MapControl;assembly=Microsoft.Maps.MapControl">
        <Grid x:Name="LayoutRoot">
        </Grid>
    </UserControl>
    
  5. Once the map control assembly is referenced, then you can add the map element <m:Map/> to your page. Put this element within the Grid.

  6. To authenticate your use of the map control, you also need to set credentials. To do this, add a CredentialsProvider attribute to the <m:Map/> element. Set the CredentialsProvider attribute to the Bing Maps Key that you obtained from the Bing Maps Account Center. Getting a Bing Maps Key is described in the Accessing the Control Using a Bing Maps Key topic. Your final MainPage.xaml looks like this:

    <UserControl x:Class="SilverlightTestApplication.MainPage"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:m="clr-namespace:Microsoft.Maps.MapControl;assembly=Microsoft.Maps.MapControl">
        <Grid x:Name="LayoutRoot">
            <m:Map CredentialsProvider="your key"/>
        </Grid>
    </UserControl>
    
  7. Run your Silverlight Map Control application to display a default map.

Change the Map Mode using XAML

To customize the map or other UIElements, you can set properties in your MainPage.xaml file.

  1. To change the mode, or style, of the map, set the Mode of the map element to “Aerial”. Your MainPage.xaml now looks like this:

    <UserControl x:Class="SilverlightTestApplication.MainPage"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:m="clr-namespace:Microsoft.Maps.MapControl;assembly=Microsoft.Maps.MapControl">
        <Grid x:Name="LayoutRoot" Background="White">
            <m:Map CredentialsProvider="your key" Mode="Aerial" />
        </Grid>
    </UserControl>
    
  2. Now run your project to see the map in Aerial mode.

Note

Not all element properties can be set in XAML.

Change the Map Mode using C# or Visual Basic

Alternatively you can use Visual C# or Visual Basic to change the map mode.

  1. Start by giving your map element a name so that you can access this element from within C# or Visual Basic. Your MainPage.xaml will look like this:

    <UserControl x:Class="SilverlightTestApplication.MainPage"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:m="clr-namespace:Microsoft.Maps.MapControl;assembly=Microsoft.Maps.MapControl">
        <Grid x:Name="LayoutRoot" Background="White">
            <m:Map CredentialsProvider="your key" Name="myMap" />
        </Grid>
    </UserControl>
    
  2. Then in your MainPage.xaml.cs (or MainPage.xaml.vb) file, reference the map control namespace with a using (or Imports) statement.

  3. Finally, set the map mode when the MainPage is constructed. Your MainPage.xaml.cs (or MainPage.xaml.vb) now looks like:

    using System.Windows.Controls;
    using Microsoft.Maps.MapControl;
    
    namespace SilverlightTestApplication
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
    
                myMap.Mode = new AerialMode();
    
            }
        }
    }
    
    Imports Microsoft.Maps.MapControl
    
    Partial Public Class MainPage
        Inherits UserControl
    
        Public Sub New()
            InitializeComponent()
            myMap.Mode = New AerialMode
        End Sub
    
    End Class
    
  4. Now run your project to see the map in Aerial mode.

See Also

Concepts

Using the Interactive SDK