/// <summary> /// Toggles the query UI on or off /// </summary> public void Execute(object parameter) { ToolExecuting = true; // Updates tool DataContext with savedConfiguration. var toolViewModel = toolView.DataContext as QueryViewModel; if (toolViewModel == null) { toolViewModel = savedConfiguration != null ? new QueryViewModel(savedConfiguration) : new QueryViewModel(); toolView.DataContext = toolViewModel; } else if (savedConfiguration != null) { toolViewModel.ApplyChanges(savedConfiguration); } // Sets map and proxy url based on application settings. if (MapApplication.Current != null) { toolViewModel.Map = MapApplication.Current.Map; if (MapApplication.Current.Urls != null) { toolViewModel.ProxyUrl = MapApplication.Current.Urls.ProxyUrl; } } // Updates default/selection on each query expression. toolViewModel.ResetExpressions(); // Sets the result layer name. toolViewModel.SetLayerNameAction = (layer, title) => { if (layer != null && !string.IsNullOrEmpty(title)) { var index = 1; string layerName = title; if (MapApplication.Current != null && MapApplication.Current.Map != null && MapApplication.Current.Map.Layers != null) { LayerCollection layers = MapApplication.Current.Map.Layers; while (layers.Any(l => MapApplication.GetLayerName(l) == layerName)) { index++; layerName = string.Format("{0} ({1})", title, index); } } MapApplication.SetLayerName(layer, layerName); } }; // Adds result layer to map. toolViewModel.AddLayerAction = (layer) => { if (layer != null) { if (MapApplication.Current.Map != null && MapApplication.Current.Map.Layers != null) { if (!MapApplication.Current.Map.Layers.Contains(layer)) { MapApplication.Current.Map.Layers.Add(layer); } } } }; // Removes result layer from map. toolViewModel.RemoveLayerAction = (layer) => { if (layer != null) { if (MapApplication.Current.Map != null && MapApplication.Current.Map.Layers != null) { if (MapApplication.Current.Map.Layers.Contains(layer)) { MapApplication.Current.Map.Layers.Remove(layer); } } } }; // Updates layer selection on map after query is executed. toolViewModel.SelectLayerAction = (layer) => { if (layer != null) { MapApplication.Current.SelectedLayer = layer; } }; // Zooms to result layer. toolViewModel.ZoomToExtentAction = (geometry) => { if (geometry != null) { var env = geometry.Extent; if (env.Width > 0 || env.Height > 0) { env = new Envelope(env.XMin - env.Width * EXPAND_EXTENT_RATIO, env.YMin - env.Height * EXPAND_EXTENT_RATIO, env.XMax + env.Width * EXPAND_EXTENT_RATIO, env.YMax + env.Height * EXPAND_EXTENT_RATIO); if (MapApplication.Current.Map != null) { MapApplication.Current.Map.ZoomTo(env); } } else { if (MapApplication.Current.Map != null) { MapApplication.Current.Map.PanTo(env); } } } }; // Updates visibility of data grid after query is expecuted. toolViewModel.UpdateDataGridVisibility = (visibility) => { UpdateFeatureDataGridVisibility(visibility); }; // Displays QueryToolView. MapApplication.Current.ShowWindow(toolViewModel.QueryTitle, toolView, false, null, (s, e) => { ToolExecuting = false; // Clears map of query results when done. toolViewModel.ClearResults(); foreach (ExpressionViewModel exp in toolViewModel.QueryExpressions) { exp.ClearValidationExceptions(); } }, WindowType.Floating); // Executes query on click when there are no visible expression. if (!toolViewModel.HasVisibleExpression) { if (toolViewModel.Execute.CanExecute(null)) { toolViewModel.Execute.Execute(null); } } }
// when layers are added/removed private void Layers_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { LayerCollection layers = (LayerCollection)sender; // Put updating of measurable layers in a throttle timer, meaning that this logic will // only be invoked once if the CollectionChanged event fires multiple times within two // tenths of a second. This is necessary because changing to a basemap in a different // spatial reference results in clearing and rebuilding the layers collection. if (_updateMeasurableLayersThrottler == null) { _updateMeasurableLayersThrottler = new ThrottleTimer(20, () => { // Check whether all layers are initialized if (layers.Any(l => !l.IsInitialized)) { layers.LayersInitialized += Layers_LayersInitialized; // Wait for initialization } else { UpdateMeasurableLayers(); // Update measurable layers collection now } }); } _updateMeasurableLayersThrottler.Invoke(); // Check whether layers were added if (e.NewItems != null) { // Hook into visibility changed events for added layers foreach (Layer layer in e.NewItems) { layer.PropertyChanged += Layer_PropertyChanged; if (layer is ArcGISDynamicMapServiceLayer) { ISublayerVisibilitySupport subLayerSupport = layer as ISublayerVisibilitySupport; subLayerSupport.VisibilityChanged += SubLayerSupport_VisibilityChanged; } } } // Check whether layers were removed if (e.OldItems != null) { // Unhook from visibility changed events for removed layers foreach (Layer layer in e.OldItems) { layer.PropertyChanged -= Layer_PropertyChanged; if (layer is ArcGISDynamicMapServiceLayer) { ISublayerVisibilitySupport subLayerSupport = layer as ISublayerVisibilitySupport; subLayerSupport.VisibilityChanged -= SubLayerSupport_VisibilityChanged; } } } // If draw layer has been added to the map, check whether it is the top-most if (layers.Contains(DrawLayer) && layers.IndexOf(DrawLayer) != layers.Count - 1) { // Draw layer is not top-most. Move it to the top by removing and re-adding it. Wrap in // a begin invoke call so the collection is modified outside the CollectionChanged event. Map.Dispatcher.BeginInvoke(() => { layers.Remove(DrawLayer); layers.Add(DrawLayer); }); } }