private void LoadMapButton_Click(object sender, RoutedEventArgs e)
        {
            WebClient webClient = new WebClient();
            string uri = string.Format("http://dev.virtualearth.net/REST/v1/Imagery/Metadata/Aerial?supressStatus=true&key={0}", BingKeyTextBox.Text);

            webClient.OpenReadCompleted += (s, a) =>
            {
                if (a.Error == null)
                {
                    DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(BingAuthentication));
                    BingAuthentication bingAuthentication = serializer.ReadObject(a.Result) as BingAuthentication;
                    a.Result.Close();

                    if (bingAuthentication.authenticationResultCode == "ValidCredentials")
                    {
                        ESRI.ArcGIS.Client.Bing.TileLayer tileLayer = new TileLayer()
                        {
                            ID = "BingLayer",
                            LayerStyle = TileLayer.LayerType.Road,
                            ServerType = ServerType.Production,
                            Token = BingKeyTextBox.Text
                        };

                        MyMap.Layers.Insert(0, tileLayer);

                        // Add your Bing Maps key in the constructor for the Routing class. Use http://www.bingmapsportal.com to generate a key.
                        routing = new ESRI.ArcGIS.Client.Bing.Routing(BingKeyTextBox.Text);
                        routing.ServerType = ServerType.Production;

                        myDrawObject = new Draw(MyMap)
                        {
                            DrawMode = DrawMode.Point,
                            IsEnabled = true
                        };

                        myDrawObject.DrawComplete += MyDrawObject_DrawComplete;
                        waypointGraphicsLayer = MyMap.Layers["WaypointGraphicsLayer"] as GraphicsLayer;
                        routeResultsGraphicsLayer = MyMap.Layers["RouteResultsGraphicsLayer"] as GraphicsLayer;

                        ESRI.ArcGIS.Client.Geometry.Envelope initialExtent =
                            new ESRI.ArcGIS.Client.Geometry.Envelope(-13064142.2218876, 3831722.30101942, -13023209.4358361, 3878454.82787388);

                        initialExtent.SpatialReference = new SpatialReference(102100);

                        MyMap.Extent = initialExtent;

                        BingKeyGrid.Visibility = System.Windows.Visibility.Collapsed;
                        DialogsGrid.Visibility = System.Windows.Visibility.Visible;

                        InvalidBingKeyTextBlock.Visibility = System.Windows.Visibility.Collapsed;

                    }
                    else InvalidBingKeyTextBlock.Visibility = System.Windows.Visibility.Visible;
                }
                else InvalidBingKeyTextBlock.Visibility = System.Windows.Visibility.Visible;
            };

            webClient.OpenReadAsync(new System.Uri(uri));
        }
        private void LoadMapButton_Click(object sender, RoutedEventArgs e)
        {
            WebClient webClient = new WebClient();
            string    uri       = string.Format("http://dev.virtualearth.net/REST/v1/Imagery/Metadata/Aerial?supressStatus=true&key={0}", BingKeyTextBox.Text);

            webClient.OpenReadCompleted += (s, a) =>
            {
                if (a.Error == null)
                {
                    DataContractJsonSerializer serializer         = new DataContractJsonSerializer(typeof(BingAuthentication));
                    BingAuthentication         bingAuthentication = serializer.ReadObject(a.Result) as BingAuthentication;
                    a.Result.Close();
                    string authenticationResult = bingAuthentication.AuthenticationResultCode.ToString();

                    if (authenticationResult == "ValidCredentials")
                    {
                        ESRI.ArcGIS.Client.Bing.TileLayer tileLayer = new TileLayer()
                        {
                            ID         = "BingLayer",
                            LayerStyle = TileLayer.LayerType.Road,
                            ServerType = ServerType.Production,
                            Token      = BingKeyTextBox.Text
                        };

                        MyMap.Layers.Insert(0, tileLayer);

                        // Add your Bing Maps key in the constructor for the Routing class. Use http://www.bingmapsportal.com to generate a key.
                        routing            = new ESRI.ArcGIS.Client.Bing.Routing(BingKeyTextBox.Text);
                        routing.ServerType = ServerType.Production;

                        myDrawObject = new Draw(MyMap)
                        {
                            DrawMode  = DrawMode.Point,
                            IsEnabled = true
                        };

                        myDrawObject.DrawComplete += MyDrawObject_DrawComplete;
                        waypointGraphicsLayer      = MyMap.Layers["WaypointGraphicsLayer"] as GraphicsLayer;
                        routeResultsGraphicsLayer  = MyMap.Layers["RouteResultsGraphicsLayer"] as GraphicsLayer;

                        ESRI.ArcGIS.Client.Geometry.Envelope initialExtent =
                            new ESRI.ArcGIS.Client.Geometry.Envelope(
                                mercator.FromGeographic(
                                    new ESRI.ArcGIS.Client.Geometry.MapPoint(-130, 20)) as MapPoint,
                                mercator.FromGeographic(
                                    new ESRI.ArcGIS.Client.Geometry.MapPoint(-65, 55)) as MapPoint);

                        initialExtent.SpatialReference = new SpatialReference(102100);

                        MyMap.Extent = initialExtent;

                        BingKeyGrid.Visibility = System.Windows.Visibility.Collapsed;
                        RouteGrid.Visibility   = System.Windows.Visibility.Visible;

                        InvalidBingKeyTextBlock.Visibility = System.Windows.Visibility.Collapsed;
                    }
                    else
                    {
                        InvalidBingKeyTextBlock.Visibility = System.Windows.Visibility.Visible;
                    }
                }
                else
                {
                    InvalidBingKeyTextBlock.Visibility = System.Windows.Visibility.Visible;
                }
            };

            webClient.OpenReadAsync(new System.Uri(uri));
        }
        private void LoadMapButton_Click(object sender, RoutedEventArgs e)
        {
            WebClient webClient = new WebClient();
            string uri = string.Format("http://dev.virtualearth.net/REST/v1/Imagery/Metadata/Aerial?supressStatus=true&key={0}", BingKeyTextBox.Text);

            webClient.OpenReadCompleted += (s, a) =>
            {
                if (a.Error == null)
                {
                    JsonValue jsonResponse = JsonObject.Load(a.Result);
                    string authenticationResult = jsonResponse["authenticationResultCode"];
                    a.Result.Close();

                    if (authenticationResult == "ValidCredentials")
                    {
                        ESRI.ArcGIS.Client.Bing.TileLayer tileLayer = new TileLayer()
                        {
                            ID = "BingLayer",
                            LayerStyle = TileLayer.LayerType.Road,
                            ServerType = ServerType.Production,
                            Token = BingKeyTextBox.Text
                        };

                        MyMap.Layers.Insert(0, tileLayer);

                        // Add your Bing Maps key in the constructor for the Routing class. Use http://www.bingmapsportal.com to generate a key.
                        routing = new ESRI.ArcGIS.Client.Bing.Routing(BingKeyTextBox.Text);
                        routing.ServerType = ServerType.Production;

                        myDrawObject = new Draw(MyMap)
                        {
                            DrawMode = DrawMode.Point,
                            IsEnabled = true
                        };

                        myDrawObject.DrawComplete += MyDrawObject_DrawComplete;
                        waypointGraphicsLayer = MyMap.Layers["WaypointGraphicsLayer"] as GraphicsLayer;
                        routeResultsGraphicsLayer = MyMap.Layers["RouteResultsGraphicsLayer"] as GraphicsLayer;

                        ESRI.ArcGIS.Client.Geometry.Envelope initialExtent =
                                new ESRI.ArcGIS.Client.Geometry.Envelope(
                            mercator.FromGeographic(
                                new ESRI.ArcGIS.Client.Geometry.MapPoint(-130, 20)) as MapPoint,
                            mercator.FromGeographic(
                                new ESRI.ArcGIS.Client.Geometry.MapPoint(-65, 55)) as MapPoint);

                        initialExtent.SpatialReference = new SpatialReference(102100);

                        MyMap.Extent = initialExtent;

                        BingKeyGrid.Visibility = System.Windows.Visibility.Collapsed;
                        RouteGrid.Visibility = System.Windows.Visibility.Visible;

                        InvalidBingKeyTextBlock.Visibility = System.Windows.Visibility.Collapsed;

                    }
                    else InvalidBingKeyTextBlock.Visibility = System.Windows.Visibility.Visible;
                }
                else InvalidBingKeyTextBlock.Visibility = System.Windows.Visibility.Visible;
            };

            webClient.OpenReadAsync(new System.Uri(uri));
        }