public WebMapTileFactory(BingMapsClient bingMapsClient)
 {
     this.bingMapsClient = bingMapsClient;
 }
        public MainWindow()
        {
            try
            {
                // setup the GPS host service 
                host = new ServiceHost(this, new Uri("http://localhost:9192/"));
                host.AddServiceEndpoint(typeof(IGpsEmulatorService), new BasicHttpBinding(BasicHttpSecurityMode.None), "GpsEmulator");
                host.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to start the GPS Emulator!\nPlease make sure the application has Administrator privilages.\n\nError details: " + ex.Message, MSG_BOX_CAPTION, MessageBoxButton.OK);
                Application.Current.Shutdown() ;
            }

            #region Read application settings from configuration file
            double lastLat, lastLng;
            int lastZoom, lastMapType;
            int windowTop, windowLeft, windowHeight, windowWidth;
            bingApiKey = ConfigurationManager.AppSettings["BingApiKey"];
            bingMapsClient = new BingApis.BingMapsClient(bingApiKey);
            if (!Double.TryParse(ConfigurationManager.AppSettings["DefaultSpeed"], out defaultSpeed)) defaultSpeed = 16.6666;
            if (!Boolean.TryParse(ConfigurationManager.AppSettings["UseRealTimeData"], out useRealTimeData)) useRealTimeData = false;
            #endregion

            InitializeComponent();

            #region BindMenuCommends

            this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Open, OpenCommandHandler, null));
            this.CommandBindings.Add(new CommandBinding(ApplicationCommands.New, NewCommendHandler, null));
            this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Save, SaveCommandHandler, null));
            this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Close, CloseCommandHandler, null));

            #endregion

            #region Restore main window settings from the last session
            if (ConfigurationManager.AppSettings["windowMaximized"] != null && ConfigurationManager.AppSettings["windowMaximized"] == "True") this.WindowState = System.Windows.WindowState.Maximized;
            if (Int32.TryParse(ConfigurationManager.AppSettings["windowTop"], out windowTop)) this.Top = windowTop;
            if (Int32.TryParse(ConfigurationManager.AppSettings["windowLeft"], out windowLeft)) this.Left = windowLeft;
            if (Int32.TryParse(ConfigurationManager.AppSettings["windowHeight"], out windowHeight)) this.Height = windowHeight > 400 ? windowHeight : 400;
            if (Int32.TryParse(ConfigurationManager.AppSettings["windowWidth"], out windowWidth)) this.Width = windowWidth > 450 ? windowWidth : 450;
            #endregion

            #region Restore map control settings from the last session
            if (!Double.TryParse(ConfigurationManager.AppSettings["LastLocationLat"], out lastLat)) lastLat = 47.6395454;
            if (!Double.TryParse(ConfigurationManager.AppSettings["LastLocationLng"], out lastLng)) lastLng = -122.130699;
            if (!Int32.TryParse(ConfigurationManager.AppSettings["LastZoom"], out lastZoom)) lastZoom = 17;
            if (!Int32.TryParse(ConfigurationManager.AppSettings["LastMapType"], out lastMapType)) lastMapType = 1;
            MapControl.ZoomLevel = lastZoom;
            MapControl.MapCenter = new Point(lastLat, lastLng);
            MapControl.MapType = (MapType) lastMapType;
            if (ConfigurationManager.AppSettings["PathColor"] != null)
            {
                Brush brush;
                switch (ConfigurationManager.AppSettings["PathColor"])
                {
                    case "Green":
                        brush = Brushes.ForestGreen;
                        break;
                    case "Blue":
                        brush = Brushes.RoyalBlue;
                        break;
                    default:
                        brush = Brushes.Red;
                        break;
                }
                MapControl.PathColor = brush;
            }
            #endregion

            MapControl.MapTileFactory = new InMemoryCacheMapTileFactory( new WebMapTileFactory(bingMapsClient) );

            // Attach map control event handlers
            MapControl.MouseMove += new System.Windows.Input.MouseEventHandler(MapControl_MouseMove);
            MapControl.MouseLeftButtonDown += new MouseButtonEventHandler(MapControl_MouseLeftButtonDown);
            MapControl.MouseLeftButtonUp += new MouseButtonEventHandler(MapControl_MouseLeftButtonUp);
            MapControl.MouseDoubleClick += new System.Windows.Input.MouseButtonEventHandler(MapControl_MouseDoubleClick);
            MapControl.MouseWheel += new MouseWheelEventHandler(MapControl_MouseWheel);

            // Bind map type drop down
            cmbMapType.ItemsSource = Enum.GetValues(typeof(Utilities.MapType));

            // route
            lvRoute.ItemsSource = route;
            lvRoute.SizeChanged += lvRoute_SizeChanged;
            lvRoute.SelectionChanged += lvRoute_SelectionChanged;
            lvRoute.KeyUp += new KeyEventHandler(lvRoute_KeyUp);

            #region Internet Connectivity Checking
            // check internet connectivity
            Task t = Task.Factory.StartNew(() =>
            {
                //bool previouslyConnectedToTheInternet = true;
                while (true)
                {
                    ((InMemoryCacheMapTileFactory)MapControl.MapTileFactory).CleanupCache();
                    tbOnlineStatus.Dispatcher.Invoke((Action)(() => {
                        tbOnlineStatus.Text = InternetConnectivityChecker.IsConnectedToInternet() ? "Connected to the Internet" : "Not Connected to the Internet";
                        if (clientCallDetected)
                        {
                            elClientIndicator.Fill = Brushes.Green;
                            tbClientIndicator.Text = "Client connected";
                            clientCallDetected = false;
                        }
                        else
                        {
                            elClientIndicator.Fill = Brushes.Red;
                            tbClientIndicator.Text = "No client connected";
                        }
                    }));
                    // Refresh the display if connected
                    //bool connectedToTheInternet = InternetConnectivityChecker.IsConnectedToInternet();
                    //if (!previouslyConnectedToTheInternet && connectedToTheInternet)
                    //{
                    //    MapControl.InvalidateVisual();
                    //}
                    //previouslyConnectedToTheInternet = connectedToTheInternet;
                    System.Threading.Thread.Sleep(2000);
                }
            }, TaskCreationOptions.LongRunning);
            #endregion
        }