Select to view content in your preferred language

Is there a feature layer "completed" event?

2772
4
09-22-2011 04:27 PM
HugoCardenas
Emerging Contributor
Is there a way to get hold of the features returned by a feature layer for further processing?

I have a feature layer which I query, not using a query task;  I use the "Where" property of the feature layer to retrieve the features at run-time.  The features, points, are drawn on screen.  I would like to get hold of those features that are drawn on screen, to avoid executing another query (a query task, perhaps) to access those attributes.

The feature layer "Where" clause retrieves the points asynchronously, so I do not know a way to get hold of those features.  I would like to declare a "Completed" event on that feature layer "Where" statement, like the Query Task does it.

This is what I do:

FeatureLayer fs = new FeatureLayer();
fs.Url = "http://.../0";
fs.Mode = FeatureLayer.QueryMode.OnDemand;
fs.Renderer = MyRenderer;
fs.Where = "AlertType = 14";
fs.OutFields.Add("*");
fs.ID = "MyMeters";
fs.Visible = true;

int index = MyMap.Layers.IndexOf(MyMap.Layers["MyMeters"]);
MyMap.Layers.RemoveAt(index);
MyMap.Layers.Insert(index, fs);

The code above retrieves and draws on the map the meters of alert type 14.  I change the alert type to 11, 12, 13, etc. using check boxes; so the "Where" statement changes.

The features are retrieved very fast.  But I need to get hold of those features, for I need to do further processing based on those attributes.

Am I using the wrong approach to query the feature layer and draw its features at run-time?
Or, is there such a thing like a "Completed" event for a feature layer?

Your response is much appreciated.

Hugo.
0 Kudos
4 Replies
SanajyJadhav
Deactivated User
You can listen to FeatureLayer::UpdateCompleted event.In this event, fresh query on the feature layer is updated.

Hope this helps.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
I agree with Sanjay.
UpdateCompleted event looks like what you are looking for.
0 Kudos
GertConradie
Frequent Contributor
UpdateFailed / UpdateCompleted tell you when the FeatureLayer is completed with retrieving the features that need to be drawn on the map.

If you want to take a image of that layer ( *.ToBitmap() ), you need to wait until the drawing is completed - I had to wait for the 1ste LayoutUpdated event of the map control after the UpdateFailed / UpdateCompleted is triggered.
0 Kudos
HugoCardenas
Emerging Contributor
Thanks Sanjay, Dominique and Gert.
I solved my problem and below is the code for the one method and its event handler (cf. code below).  Yes, "UpdateCompleted" did it.

The collection sent by the feature service keeps its reference all the way through; one cannot just assign the returned graphics to the existing graphics layer bound to the feature data grid.  I had to create a nested loop (cf. FeatureLayer_UpdateCompleted) and copy the geometry and attributes to the new graphic.  Then, I inserted the new graphic to the existing graphics layer bound to the feature data grid.  (I learned from Dominique not to use "ItemsSource" to bind features to the feature data grid: update the graphics layer associated with it, instead.)

Thanks a lot!
Hugo.

public void ReLoadMeters(string sql)
    {
      /* Retrieve meters for the alert type check box status */
      ESRI.ArcGIS.Client.FeatureLayer fs = new ESRI.ArcGIS.Client.FeatureLayer();

      // Set event handler on feature layer feature retrieval.
      fs.UpdateCompleted += FeatureLayer_UpdateCompleted;

      fs.Url = MetersFeatureService;
      if (!string.IsNullOrWhiteSpace(sql)) fs.Where = sql;
      fs.Mode = FeatureLayer.QueryMode.OnDemand;
      fs.Renderer = AlertTypesRenderer;
      fs.OutFields.Add("*");
      fs.Visible = true;
      fs.ID = MetersMapName;

      // Add the new meters to the map. Remove the old layer first
      int index = MyMap.Layers.IndexOf(MyMap.Layers[MetersMapName]);
      if (index >= 0)
      {
        try
        {
          MyMap.Layers.RemoveAt(index);
          MyMap.Layers.Insert(index, fs);
        }
        catch { }
      }
    }


    private void FeatureLayer_UpdateCompleted(object sender, EventArgs args)
    {
      // Create instance of feature layer that just returned features from ArcGIS Cloud Server
      ESRI.ArcGIS.Client.FeatureLayer fs = sender as ESRI.ArcGIS.Client.FeatureLayer;

      // Clear graphics layer bound to feature data grid
      _myFeatureDataGridLayer.ClearGraphics();

      // Fill graphics layer for feature data grid 
      // Iterate through every feature sent by feature service
      foreach (ESRI.ArcGIS.Client.Graphic graphic in fs.Graphics)
      {
        // Create a new graphic instance to prevent OutOfRangeException silent errors.
        ESRI.ArcGIS.Client.Graphic gr = new ESRI.ArcGIS.Client.Graphic();

        // Retrieve geometry
        gr.Geometry = graphic.Geometry;

        // Iterate through every attribute and copy it to new graphic.
        foreach (KeyValuePair<string, object> kvp in graphic.Attributes)
        {
          gr.Attributes.Add(kvp);
        }

        // Insert new graphic to graphics layer bound to feature data grid
        _myFeatureDataGridLayer.Graphics.Insert(0, gr);
      }
    }
0 Kudos