AddVertex issue

3138
1
Jump to solution
11-18-2014 11:17 AM
KeithMacIntyre
New Contributor III

We would like to add a vertex to the editor from code behind.  We would like the first point to be fixed and the user select the second point.  We are unable to get the AddVertex command to execute.

Task t = this.Editor.RequestShapeAsync(Esri.ArcGISRuntime.Controls.DrawShape.Polygon);

this.Editor.AddVertex.Execute(new MapPoint(0, 0));

We receive an ArgumentException was unhandled by user code, "Cannot execute command."

0 Kudos
1 Solution

Accepted Solutions
GregDeStigter
Esri Contributor

The problem here seems to be that the vertex is added too quickly - before the Editor has a chance to initialize its drawing resources.  In this case, the MapView.Editor.AddVertex.CanExecute method would return false.

Try to wait for the Editor to catch up like this instead:

// Start the Editor polygon drawing process

var tcs = new TaskCompletionSource<bool>();

EventHandler handler = (s, a) => { tcs.SetResult(true); };

MyMapView.Editor.AddVertex.CanExecuteChanged += handler;

var drawTask = MyMapView.Editor.RequestShapeAsync(DrawShape.Polygon);

await tcs.Task;

MyMapView.Editor.AddVertex.CanExecuteChanged -= handler;

// After Editor initialization, add a fixed initial point

MyMapView.Editor.AddVertex.Execute(MyMapView.Extent.GetCenter());

// Now get the rest of the points from the user

var poly = await drawTask as Polygon;

This should allow you to set a fixed starting point for the polygon and let the user fill in the rest.

-Greg

View solution in original post

1 Reply
GregDeStigter
Esri Contributor

The problem here seems to be that the vertex is added too quickly - before the Editor has a chance to initialize its drawing resources.  In this case, the MapView.Editor.AddVertex.CanExecute method would return false.

Try to wait for the Editor to catch up like this instead:

// Start the Editor polygon drawing process

var tcs = new TaskCompletionSource<bool>();

EventHandler handler = (s, a) => { tcs.SetResult(true); };

MyMapView.Editor.AddVertex.CanExecuteChanged += handler;

var drawTask = MyMapView.Editor.RequestShapeAsync(DrawShape.Polygon);

await tcs.Task;

MyMapView.Editor.AddVertex.CanExecuteChanged -= handler;

// After Editor initialization, add a fixed initial point

MyMapView.Editor.AddVertex.Execute(MyMapView.Extent.GetCenter());

// Now get the rest of the points from the user

var poly = await drawTask as Polygon;

This should allow you to set a fixed starting point for the polygon and let the user fill in the rest.

-Greg

View solution in original post