/// <summary> /// Performs cleanup logic such as unhooking event handlers. Should be invoked when /// the <see cref="MeasureViewModel"/> instance is no longer in use. /// </summary> internal void Deactivate() { Map.Layers.CollectionChanged -= Layers_CollectionChanged; Map.ExtentChanged -= Map_ExtentChanged; DrawObject.DrawComplete -= DrawObject_DrawComplete; // unhook from visibility changes foreach (Layer layer in Map.Layers) { layer.PropertyChanged -= Layer_PropertyChanged; if (layer is ArcGISDynamicMapServiceLayer) { ISublayerVisibilitySupport subLayerSupport = layer as ISublayerVisibilitySupport; subLayerSupport.VisibilityChanged -= SubLayerSupport_VisibilityChanged; } } }
public MeasureViewModel(Map map, string geometryServiceUrl) { _linearUnits = Utils.GetEnumDescriptions <LengthUnits>(); _arealUnits = Utils.GetEnumDescriptions <AreaUnits>(); LinearUnit = LengthUnits.UnitsMeters; ArealUnit = AreaUnits.UnitsSquareMeters; Map = map; map.Layers.CollectionChanged += Layers_CollectionChanged; map.ExtentChanged += Map_ExtentChanged; GeometryServiceUrl = geometryServiceUrl; DrawLayer = new GraphicsLayer(); // Initialize the draw object. Hook to DrawComplete to update the ViewModel's // DrawGeometry. DrawObject = new Draw(Map); DrawObject.DrawComplete += DrawObject_DrawComplete; // Bind the FreehandDrawMode attached property on the Draw object to the ViewModel's // FreehandDrawMode property. Pushes updates to the ViewModel's property through to // the attached property. Binding b = new Binding("FreehandDrawMode") { Source = this }; BindingOperations.SetBinding(DrawObject, AttachedProperties.FreehandDrawModeProperty, b); // hook in for layer visibility changes foreach (Layer layer in map.Layers) { layer.PropertyChanged += Layer_PropertyChanged; if (layer is ArcGISDynamicMapServiceLayer) { ISublayerVisibilitySupport subLayerSupport = layer as ISublayerVisibilitySupport; subLayerSupport.VisibilityChanged += SubLayerSupport_VisibilityChanged; } } }
// 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); }); } }