Select to view content in your preferred language

Programatically Adding a Feature Layer

2653
9
09-14-2011 11:55 AM
BrianGustafson
Occasional Contributor
I have a feature layer that I am trying to add in code and then add a click event to that layer.  It has worked in the past but for some reason it is no longer working, no events are fired when I click on the layer.  I have another feature layer that I added in XAML that uses the same event and that layer works.

_fLayer = new FeatureLayer();
            _fLayer.ID = AvailParcels.SelectedItem.ToString();
            int iID = GetParcelLayerId(_fLayer.ID);
            _fLayer.Url = "xxxx" + iID;
            IRenderer _renderer = LayoutRoot.Resources["MyRenderer"] as IRenderer;
            _fLayer.Renderer = _renderer;
            OutFields _outFields = new OutFields();
            _outFields.Add("*");
            _fLayer.MinimumResolution = .000001;
            _fLayer.MaximumResolution = 45;
            //getDisplayField(_fLayer.Url);
            _fLayer.OutFields = _outFields;
            _fLayer.Mode = FeatureLayer.QueryMode.OnDemand;
            _fLayer.MouseLeftButtonUp += new GraphicsLayer.MouseButtonEventHandler(FeatureLayer_MouseLeftButtonUp);
0 Kudos
9 Replies
BrianGustafson
Occasional Contributor
I got my issue solved.  The issue was that I had a notify property changed on the layer collection and when it ran it was thinking that the feature layer was a graphics layer and then drawing the feature layer and the graphics layer on top of my feature layer blocking it from the mouseup event.  By checking if it was a feature layer first i was able to kick out of that function.
0 Kudos
HugoCardenas
Emerging Contributor
Brian,
For some odd reason I am unable to load my Feature Layer at run-time.  This is some of my code:

FeatureLayer fs = new FeatureLayer();
fs.Url = "http://50...../FeatureServer/0";  // here, I intentionally removed the inner url data
fs.Renderer = LayoutRoot.Resources["AlertTypesRenderer"] as IRenderer;
fs.OutFields.Add("*");
fs.ID = "SmallLeaks";
fs.Initialize();
MyMap.Layers.Add(fs);

Nothing is added to the map.  But it works fine in XAML code:

<esri:FeatureLayer ID="MyMeters" Renderer="{StaticResource AlertTypesRenderer}" Url="http://50..../FeatureServer/0" OutFields="*"  />

Would you, or anyone else, know as to why the code-behind (C#) returns nothing, while the XAML code returns nothing.

I need the run-time option, because I have check boxes that control which values to use to display my points; I would like to use:

fs.Where = "AlertType = 14";  // for one check box
fs.Where = "AlertType = 11";  // for another check box
and son on.

But, I cannot even see any of the points!

Hugo.
0 Kudos
BrianGustafson
Occasional Contributor
Have you tried looping through your layer collection just for debugging purposes to make sure that the layer is either missing from the collection or does it exist but is it displayed behind another layer?  When you add it in XAML if it works try using MyMap.Layers.Insert(0, fs); so that the layer is added to the map on top.
0 Kudos
HugoCardenas
Emerging Contributor
Brian,
I have.  The feature layer is added to the map layers collection.  It is there, but there isnothing in it.
Also, I debug into it, prior to adding to the map layers collection, and I see the "Graphics" collection is empty:

IEnumerable<Graphic> graphics = fs.Graphics;

I can connect directly to the feature service using the browser.  I can query it, with the browser.  I can display it using the XAML code; but I cannot load the feature service programatically.

Thanks for your reply!
Hugo.
0 Kudos
DominiqueBroux
Esri Frequent Contributor
Also, I debug into it, prior to adding to the map layers collection, and I see the "Graphics" collection is empty:

That's normal. The layer is populated asynchrounously after it's initialized.

Using fiddler may give you a clue about the issue.

At first glance, I don't see why the result would be different because you created the layer by code. Just check that the renderer is not null (fs.Renderer = LayoutRoot.Resources["AlertTypesRenderer"] as IRenderer;)
0 Kudos
HugoCardenas
Emerging Contributor
Thanks Dominique!
You are right, again...
The IRenderer was being returned null.

To solve the problem, I set the "Name" property of the renderer in the XAML code:

Before:
<esri:UniqueValueRenderer x:Key="AlertTypesRenderer" Attribute="AlertType" >

Now:
<esri:UniqueValueRenderer x:Key="AlertTypesRenderer" x:Name="AlertTypesRenderer" Attribute="AlertType" >

In C#, I reference the IRenderer property "Name", rather than the key:

fs.Renderer = AlertTypesRenderer;

Thanks to all of you!

Hugo.
0 Kudos
JonathanHouck
Occasional Contributor
I'm having a hard time adding a feature layer in code behind.  I'm trying to add a feature layer based on the user's selection from a combobox:

 if (LayerComboBox.SelectedItem != "Select...")
            {
                FeatureLayer queryLayer = new FeatureLayer();
                string layerComboBoxItem;
                string layerID;
                layerComboBoxItem = LayerComboBox.SelectedItem.ToString();
                layerID = GetLayerID(layerComboBoxItem).ToString();
                queryLayer.ID = "QueryLayer";
                queryLayer.Url = "http://......./rest/services/QueryLayerService/MapServer/" + layerID;
                queryLayer.OutFields.Add("*");
                MyMap.Layers.Add(queryLayer);
                queryLayer.Initialize();
            }
        }


I think it's adding it, but when I get to the next portion of the app (where I populate a combo box with field names):

QueryFormCombo1.ItemsSource = (MyMap.Layers["QueryLayer"] as FeatureLayer).LayerInfo.Fields;


It gives me a null reference exception.  After looking into it, LayerInfo is null, and I can't seem to figure out how to use the FeatureLayerInfo.Get like it says in the tool tip.  Can someone point me in the right direction? Thanks!
0 Kudos
DominiqueBroux
Esri Frequent Contributor
LayerInfos is null as long as the layer is not initialized.

Hook up an handler to event 'Initialized' and populate your combo box at this time.
0 Kudos
HugoCardenas
Emerging Contributor
Jonathan,
Check out the following link.  I was having the same problem.  Here I described how to solve it (cf. the last post):

http://forums.arcgis.com/threads/40125-Is-there-a-feature-layer-quot-completed-quot-event

Hugo.
0 Kudos