Trouble creating a featurelayer programatically

1844
2
05-10-2012 04:54 AM
CraigPatterson
New Contributor III
Trouble Instantiating a FeatureLayer Programatically

    I'm trying to create a featurelayer programatically. Unfortunately, it doesn't seem to get instantiated. I have built a usercontrol and in it's constructor, I create the object like this.

            private FeatureLayer securityFeatureLayer;

            public SecurityControl()
            {
                InitializeComponent();

                securityFeatureLayer = new FeatureLayer()
                {
                    Url = "http://xxxxxxxxx/FeatureServer/1",
                    AutoSave = false,
                    Where="1=1"

                };

                securityFeatureLayer.UpdateCompleted += securityFeatureLayer_UpdateCompleted;

                securityFeatureLayer.Update();

            }





    The UpdateCompleted handler then attempts to set a feature data grid GraphicsLayer to the feature layer.


            private void securityFeatureLayer_UpdateCompleted(object sender, EventArgs e)
            {

                SecurityFeatureDataGrid.GraphicsLayer = securityFeatureLayer;

            }


    When I debug it, it never reaches the handler so it seems like I'm missing a step.

    Any help is greatly appreciated.

    Craig
0 Kudos
2 Replies
DarinaTchountcheva
Occasional Contributor II
Craig,

I had similar problem a few years ago, and I think that you have to call the Update method after the feature layer has been initialized.
So, try this:

public SecurityControl()
            {
                InitializeComponent();

                securityFeatureLayer = new FeatureLayer()
                {
                    Url = "http://xxxxxxxxx/FeatureServer/1",
                    AutoSave = false,
                    Where="1=1"

                };

                securityFeatureLayer.UpdateCompleted += securityFeatureLayer_UpdateCompleted;
                securityFeatureLayer.Initialized += securityFeatureLayer_Initialized;
                

            }

            private void securityFeatureLayer_Initialized(object sender, EventArgs args)
            {               
                securityFeatureLayer.Update();
            }


Good Luck!
0 Kudos
JenniferNery
Esri Regular Contributor
If your layer is added to your map control, it is initialized when map is initialized. Otherwise, you need to be explicit and call layer.Initialize() and only until then can you call layer.Update(). This seems related (post# 16) forums.arcgis.com/threads/21714-Editing-Tables-in-Feature-Service-with-Silverlight-4
0 Kudos