private async void Page_Loaded(object sender, RoutedEventArgs e) { string serviceBusConnectionString = new ServiceBusConnectionStringBuilder(servicebusEndpoint, String.Empty, sharedAccessKeyName, sharedAccessKey).ToString(); queueClient = new QueueClient(serviceBusConnectionString, queueName, ReceiveMode.PeekLock); // Handle messages received from the Service Bus queue queueClient.RegisterMessageHandler( async(message, token) => { // Deserialize the data as patrol car data string data = Encoding.UTF8.GetString(message.Body); int pos = data.LastIndexOf('{'); data = data.Substring(pos); PatrolCarData patrolCarData = JsonConvert.DeserializeObject <PatrolCarData>(data); // Plot the position of the patrol car on the map await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { MapIcon patrolCarIcon = new MapIcon(); BasicGeoposition patrolCarPosition = new BasicGeoposition() { Latitude = patrolCarData.LocationLatitude, Longitude = patrolCarData.LocationLongitude }; // If the patrol car already exists then just update its location MapIcon car = (MapIcon)MapControl.MapElements.FirstOrDefault(m => String.Compare((m as MapIcon).Title, patrolCarData.CarID) == 0); if (car != null) { car.Location = new Geopoint(patrolCarPosition); } // Otherwise create a new icon and add it to the map else { patrolCarIcon.Location = new Geopoint(patrolCarPosition); patrolCarIcon.NormalizedAnchorPoint = new Point(0.5, 1.0); patrolCarIcon.Title = patrolCarData.CarID; patrolCarIcon.ZIndex = 0; patrolCarIcon.Image = patrolCarImage; MapControl.MapElements.Add(patrolCarIcon); } }); await queueClient.CompleteAsync(message.SystemProperties.LockToken); }, new MessageHandlerOptions(async(args) => { // Error handling - discard the message }) ); }
private async void Page_Loaded(object sender, RoutedEventArgs e) { string serviceBusConnectionString = new ServiceBusConnectionStringBuilder(servicebusEndpoint, String.Empty, sharedAccessKeyName, sharedAccessKey).ToString(); queueClient = new QueueClient(serviceBusConnectionString, queueName, ReceiveMode.PeekLock); cameraQueueClient = new QueueClient(serviceBusConnectionString, cameraQueueName, ReceiveMode.PeekLock); // Handle messages received from the Service Bus patrol car queue queueClient.RegisterMessageHandler( async(message, token) => { // Deserialize the data as patrol car data string data = Encoding.UTF8.GetString(message.Body); // Remove any leading preamble, before the start of the JSON text int pos = data.LastIndexOf('{'); data = data.Substring(pos); // Remove any extraneous content after the closing } of the JSON text pos = data.LastIndexOf('}'); if (pos != 0) { data = data.Substring(0, pos + 1); } PatrolCarData patrolCarData = JsonConvert.DeserializeObject <PatrolCarData>(data); // Plot the position of the patrol car on the map await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { BasicGeoposition patrolCarPosition = new BasicGeoposition() { Latitude = patrolCarData.LocationLatitude, Longitude = patrolCarData.LocationLongitude }; // If the patrol car already exists then just update its location MapIcon car = (MapIcon)MapControl.MapElements.FirstOrDefault(m => String.Compare((m as MapIcon).Title, patrolCarData.CarID) == 0); if (car != null) { car.Location = new Geopoint(patrolCarPosition); } // Otherwise create a new icon and add it to the map else { car = new MapIcon(); car.Location = new Geopoint(patrolCarPosition); car.NormalizedAnchorPoint = new Point(0.5, 1.0); car.Title = patrolCarData.CarID; car.ZIndex = 0; car.Image = patrolCarImage; MapControl.MapElements.Add(car); } }); await queueClient.CompleteAsync(message.SystemProperties.LockToken); }, new MessageHandlerOptions(async(args) => { // Error handling - discard the message }) ); // Handle messages received from the Service Bus speed camera queue cameraQueueClient.RegisterMessageHandler( async(message, token) => { // Deserialize the data as speed camera data string data = Encoding.UTF8.GetString(message.Body); // Remove any leading preamble, before the start of the JSON text int pos = data.LastIndexOf('{'); data = data.Substring(pos); // Remove any extraneous content after the closing } of the JSON text pos = data.LastIndexOf('}'); if (pos != 0) { data = data.Substring(0, pos + 1); } SpeedCameraData speedCameraData = JsonConvert.DeserializeObject <SpeedCameraData>(data); // Plot the position of the speed camera on the map await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { BasicGeoposition speedCameraPosition = new BasicGeoposition() { Latitude = speedCameraData.LocationLatitude, Longitude = speedCameraData.LocationLongitude }; // If the speed camera already exists then just update its observation MapIcon speedCamera = (MapIcon)MapControl.MapElements.FirstOrDefault(m => String.Compare((m as MapIcon).Title, speedCameraData.CameraID) == 0); if (speedCamera != null) { speedCamera.Title = $"Speed: {speedCameraData.Speed.ToString()} mph"; } // Otherwise create a new icon and add it to the map else { speedCamera = new MapIcon(); speedCamera.Location = new Geopoint(speedCameraPosition); speedCamera.NormalizedAnchorPoint = new Point(0.5, 1.0); speedCamera.Title = $"Speed: {speedCameraData.Speed.ToString()} mph"; speedCamera.ZIndex = 0; speedCamera.Image = speedCameraImage; MapControl.MapElements.Add(speedCamera); } }); await cameraQueueClient.CompleteAsync(message.SystemProperties.LockToken); }, new MessageHandlerOptions(async(args) => { // Error handling - discard the message }) ); }