Measure Action in Code

1633
9
05-01-2010 12:32 PM
LanceCrumbliss
Occasional Contributor II
hi all,

i'm trying to utilize the measure action in code behind. i've got a button that will show a user control with two radio buttons to choose the measure mode (distance or area) then a combo box to select the untis (dependent on the measure mode).

i'd like to 1) instantiate (if that's the correct word) the behavior in code behind and 2) have the measuring action persist for as long as the user control window is open.  not sure how to go about doing those, so any help would be appreciated. here is what i have so far. also, how do i set the TargetName property of the Measure action?

Partial Public Class Measure
 Inherits UserControl
 Private m As New ESRI.ArcGIS.Client.Actions.MeasureAction

 Public Shared ReadOnly MapProperty As DependencyProperty = DependencyProperty.Register("Map", GetType(ESRI.ArcGIS.Client.Map), GetType(Measure), Nothing)
 Public Property Map() As ESRI.ArcGIS.Client.Map
  Get
   Return DirectCast(GetValue(MapProperty), ESRI.ArcGIS.Client.Map)
  End Get
  Set(ByVal value As ESRI.ArcGIS.Client.Map)
   SetValue(MapProperty, value)
  End Set
 End Property

 Public Sub New()
  InitializeComponent()
  m.AreaUnit = ESRI.ArcGIS.Client.Actions.AreaUnit.SquareMiles
  m.DistanceUnit = ESRI.ArcGIS.Client.Actions.DistanceUnit.Feet
                                m.MapUnits = ESRI.ArcGIS.Client.Actions.DistanceUnit.Meters
  m.LineSymbol = DefaultLineSymbol
  m.FillSymbol = DefaultFillSymbol
  m.DisplayTotals = True
  m.MeasureMode = ESRI.ArcGIS.Client.Actions.MeasureAction.Mode.Polyline
 End Sub

 Private Sub cbUnits_SelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
  'ToDo
 End Sub

 Private Sub RadioButton_Checked(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
  'ToDo
 End Sub
End Class


lance
0 Kudos
9 Replies
LanceCrumbliss
Occasional Contributor II
is this even possible or am i wasting my time?

lance
0 Kudos
DominiqueBroux
Esri Frequent Contributor
I am not sure that it's recommended to use action in code, but, if it's needed, you can do this (in C# sorry):

1) Create a new action class  (the goal is just to be able to call 'Invoke' which is protected):
 public class MyMeasureAction : MeasureAction
 {
  public void Execute()
  {
   Invoke(null);
  }
 }


2) Call this class when you want to activate the action:
  private void Measure_Click(object sender, System.Windows.RoutedEventArgs e)
  {
   MyMeasureAction m = new MyMeasureAction();

   m.MeasureMode = MeasureAction.Mode.Polygon;
   m.AreaUnit = AreaUnit.SquareKilometers;
   m.DisplayTotals = true;
   m.FillSymbol = DefaultFillSymbol;
   m.DistanceUnit = DistanceUnit.Meters;
   m.Attach(MyMap);
   m.Execute();
  }


/Dominique
0 Kudos
LanceCrumbliss
Occasional Contributor II
that's the trick!  thanks so much!
0 Kudos
SethPatrich
New Contributor III
Is it possible to invoke the MeasureAction in the C# code with the ESRI Silverlight 2.0 API?

Thanks,
Seth
0 Kudos
DominiqueBroux
Esri Frequent Contributor
The code given is this thread is supposed to work with the ESRI Silverlight 2.0 API.
Did you try it?
0 Kudos
PLadd
by
Occasional Contributor III
That's awesome.  Thanks Dominique.

I needed this for when client has used selection tools and then tries to measure.  Here's what I had to do to get it to work:

public class myMeasureAction : ESRI.ArcGIS.Client.Actions.MeasureAction
        {
            public void Execute()
            {
                Invoke(null);
            }
        }

private void btnMeasureLength_mi_Click(object sender, RoutedEventArgs e)
        {
            MyDrawSurface.DrawMode = DrawMode.None;  //stops selection tools from working

            myMeasureAction m = new myMeasureAction();
            m.MeasureMode = myMeasureAction.Mode.Polyline;
            m.DisplayTotals = true;
            m.DistanceUnit = ESRI.ArcGIS.Client.Actions.DistanceUnit.Miles;
            m.Attach(MyMap);
            m.Execute();
        }
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Glad it was useful for you 🙂

Thanks
0 Kudos
DanDong
New Contributor
Glad it was useful for you 🙂

Thanks


Hi Dominique, do you know which piece of codes shows this measure action is completed? I am thinking adding something when this action is finished. Thank you!
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Hi Dominique, do you know which piece of codes shows this measure action is completed?


As far as I know there is no event coming from the measure action  to show that the measure is over.

The idea coming from this thread : http://forums.arcgis.com/threads/5650-MeasureAction-action-finished-event is to use the map.Layers events but there is perhaps better idea.
0 Kudos