Customizing available geometry collection methods

The CollectFeaturesTask class contains an event—CreatingGeometryCollectionMethods—that is raised when you choose to collect feature geometry and are prompted to choose which collection methoduse. Listening to this event allows a developer to customize the available collection methods, either removing an existing geometry collection method or adding a custom geometry collection method.

public event  EventHandler<GeometryCollectionStartedEventArgs>  GeometryCollectionStarted;
public event EventHandler<GeometryCollectionCompletedEventArgs> GeometryCollectionCompleted;

The CreatingGeometryCollectionMethodsEventArgs class contains a list of GeometryCollectionMethod objects available for collecting a feature geometry.

public  GeometryCollectionMethod CollectionMethod { get; protected set; }

The following is an example of an extension that listens to the CreatingGeometryCollectionMethods event and modifies the available geometry collection methods:

public class  GeometryMethodCustomizationExtension : ProjectExtension
{
  private CollectFeaturesTask _collectFeaturesTask;

  protected override void Initialize()
  {
  }

  protected override void OnOwnerInitialized()
  {
    _collectFeaturesTask = MobileApplication.Current.FindTask(typeof(CollectFeaturesTask)) as CollectFeaturesTask;
    if (_collectFeaturesTask != null)
    {
      _collectFeaturesTask.GeometryCollectionStarted += new EventHandler<GeometryCollectionStartedEventArgs>(_collectFeaturesTask_GeometryCollectionStarted);
      _collectFeaturesTask.GeometryCollectionCompleted += new EventHandler<GeometryCollectionCompletedEventArgs>(_collectFeaturesTask_GeometryCollectionCompleted);
    }
  }

  protected override void Uninitialize()
  {
    if (_collectFeaturesTask != null)
    {
      _collectFeaturesTask.GeometryCollectionStarted -= new EventHandler<GeometryCollectionStartedEventArgs>(_collectFeaturesTask_GeometryCollectionStarted);
      _collectFeaturesTask.GeometryCollectionCompleted -= new EventHandler<GeometryCollectionCompletedEventArgs>(_collectFeaturesTask_GeometryCollectionCompleted);
      _collectFeaturesTask = null;
    }
  }

  void _collectFeaturesTask_GeometryCollectionStarted(object sender, GeometryCollectionStartedEventArgs e)
  {
    StringBuilder builder = new StringBuilder();
    builder.AppendLine(String.Format("Collecting a new geometry by {0}", e.CollectionMethod.Name));
    builder.AppendLine(String.Format("Feature type = {0}", e.CollectionMethod.Feature.FeatureType.Name));
    ESRI.ArcGIS.Mobile.Client.Windows.MessageBox.ShowDialog(builder.ToString());
  }

  void _collectFeaturesTask_GeometryCollectionCompleted(object sender, GeometryCollectionCompletedEventArgs e)
  {
    StringBuilder builder = new StringBuilder();
    if (e.Canceled)
    {
      builder.AppendLine("Geometry collection has been canceled");
    }
    else
    {
      builder.AppendLine(String.Format("Finished collecting {0} geometry.", e.CollectionMethod.Geometry.GeometryType.ToString()));
      int partCount = 0;
      int coordinateCount = 0;
      foreach (ESRI.ArcGIS.Mobile.Geometries.CoordinateCollection coordinates in e.CollectionMethod.Geometry.Parts)
      {
        partCount++;
        coordinateCount += coordinates.Count;
      }
      builder.AppendLine(String.Format("Geometry contains {0} parts and {1} coordinates total", partCount, coordinateCount));
    }
    ESRI.ArcGIS.Mobile.Client.Windows.MessageBox.ShowDialog(builder.ToString());
  }
}


9/20/2011