private static void SelectedBusStopChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) { BusStopControlViewModel lastSelected = args.OldValue as BusStopControlViewModel; if (lastSelected != null) { lastSelected.IsSelected = false; } BusStopControlViewModel newSelected = args.NewValue as BusStopControlViewModel; if (newSelected != null) { newSelected.IsSelected = true; // Make sure the new view model is bound to an existing control. // If it's not then we need to find the one that is and make it selected: MapControl mapControl = (MapControl)dependencyObject; var busStop = (from child in mapControl.map.Children let currentBusStop = child as BusStop where currentBusStop != null let busStopControlViewModel = currentBusStop.ViewModel where busStopControlViewModel != null && string.Equals(busStopControlViewModel.StopId, newSelected.StopId, StringComparison.OrdinalIgnoreCase) && busStopControlViewModel != newSelected select currentBusStop).FirstOrDefault(); // This means we have a control that matches the selected control, but it is not // the same view model. if (busStop != null) { busStop.ViewModel = newSelected; } } }
/// <summary> /// Clears the view model when we're unloaded. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnUnloaded(object sender, RoutedEventArgs e) { if (this.viewModel != null) { this.viewModel.PropertyChanged -= OnViewModelPropertyChanged; this.viewModel = null; } }
private static async void BusStopsChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) { var mapControl = dependencyObject as MapControl; if (args.NewValue == null) { mapControl.displayedBusStopLookup.Clear(); mapControl.map.Children.Clear(); mapControl.map.Children.Add(mapControl.userLocationIcon); MapLayer.SetPosition(mapControl.userLocationIcon, mapControl.userLocation); } else { MapControlViewModel mapControlViewModel = (MapControlViewModel)mapControl.DataContext; var stops = args.NewValue as BusStopList; if (stops.ClearExistingStops) { // If the clear existing stops property is set to true, // then we should clear the existing stops: mapControl.map.Children.Clear(); mapControl.displayedBusStopLookup.Clear(); } foreach (var stop in stops) { // If we're not already displaying this bus stop then add it to the list: if (!mapControl.displayedBusStopLookup.Contains(stop.StopId)) { BusStopControlViewModel busStopControlViewModel = new BusStopControlViewModel(stop); if (mapControlViewModel.SelectedBusStop != null) { busStopControlViewModel.IsSelected = string.Equals(mapControlViewModel.SelectedBusStop.StopId, stop.StopId, StringComparison.OrdinalIgnoreCase); if (busStopControlViewModel.IsSelected) { mapControlViewModel.SelectedBusStop = busStopControlViewModel; } } BusStop busStopIcon = new BusStop(); busStopIcon.ViewModel = busStopControlViewModel; mapControl.map.Children.Add(busStopIcon); // Wait for the UI to idle before we add another bus stop to make the UI more responsive: await mapControl.uiHelper.WaitForIdleAsync(); mapControl.displayedBusStopLookup.Add(stop.StopId); MapLayer.SetPosition(busStopIcon, new Location(stop.Latitude, stop.Longitude)); } } } }