adding label dynamically in silverlight

2865
1
05-15-2015 10:19 AM
LuChen
by
New Contributor

Hello everyone,

I am trying to change the sample codes to run time labeling function, which needs to enable labels to show when you click a button

ArcGIS API for Silverlight - Interactive Samples | ArcGIS for Developers ---this is the sample I was trying to modify.

and the result I got from step-by-step debugging looks fine. but I don't see any labels show up to my map

here are my codes:

  private void Button_Click(object sender, RoutedEventArgs e)

        {

            ArcGISDynamicMapServiceLayer DLayer = new ArcGISDynamicMapServiceLayer();

            DLayer = (ArcGISDynamicMapServiceLayer)MyMap.Layers["PDLayer"];

            DLayer.LayerDrawingOptions = new LayerDrawingOptionsCollection();

            LayerDrawingOptions layerdrawingoptions = new LayerDrawingOptions()

            {

                LayerID = 1,

                ShowLabels = true,

                ScaleSymbols = true

            };

            LabelClassCollection labelclasscollection = new LabelClassCollection();

            LabelClass labelclass1 = new LabelClass()

            {

                LabelExpression = "[POP2000]",

                LabelPlacement = LabelPlacement.LinePlacementAboveAfter,

                Where = "[POP2000] <= 61",

                MinScale = 5000,

                LabelOptions = new LabelOptions()

                {

                    Color = Colors.Red,

                    FontFamily = new FontFamily("Arial"),

                    FontSize = 10,

                    FontWeight = FontWeights.Bold

                }

            };

            LabelClass labelclass2 = new LabelClass()

            {

                LabelExpression = "[POP2000]",

                LabelPlacement = LabelPlacement.LinePlacementAboveAfter,

                Where = "[POP2000] > 61 AND [POP2000] <264",

                MinScale = 15000,

                LabelOptions = new LabelOptions()

                {

                    Color = Colors.Black,

                    FontFamily = new FontFamily("Arial"),

                    FontSize = 10,

                    FontWeight = FontWeights.Bold

                }

            };

            LabelClass labelclass3 = new LabelClass()

            {

                LabelExpression = "[POP2000]",

                LabelPlacement = LabelPlacement.LinePlacementAboveAfter,

                Where = "[POP2000] > 264",

                MinScale = 30000,

                LabelOptions = new LabelOptions()

                {

                    Color = Colors.Blue,

                    FontFamily = new FontFamily("Arial"),

                    FontSize = 14,

                    FontWeight = FontWeights.Bold

                }

            };

            labelclasscollection.Add(labelclass1);

            labelclasscollection.Add(labelclass2); //=labeldrawingoptions.labelclass.add()

            labelclasscollection.Add(labelclass3);

            layerdrawingoptions.LabelClasses = labelclasscollection;

            DLayer.LayerDrawingOptions.Add(layerdrawingoptions);

            DLayer.Refresh();

            MyMap.UpdateLayout();

        }

Tags (2)
0 Kudos
1 Reply
saiedaon
New Contributor

   try this code

// ShowLayersLabel(true, LayerLabelField);

// true to show label , false to hide labels

//  string LayerLabelField = "[Name]";

// ArcGISDynamicMapServiceLayer flayer;

  private void ShowLayersLabel(bool p, string LayerLField)

        {

            DynamicLayerInfoCollection myDynamicLayerInfoCollection = null;

            myDynamicLayerInfoCollection = flayer.CreateDynamicLayerInfosFromLayerInfos();

            if (myDynamicLayerInfoCollection != null)

            {

                DynamicLayerInfo myDynamicLayerInfo = myDynamicLayerInfoCollection.FirstOrDefault();

                LabelClass ls = new LabelClass();

                ls.LabelExpression = LayerLField;

                ls.LabelPlacement = LabelPlacement.PolygonPlacementAlwaysHorizontal;

                LabelOptions lpo = new LabelOptions()

                {

                    Color = Colors.Red,

                    FontSize = 10,

                    FontStyle = TextStyle.Normal,

                    FontFamily = new System.Windows.Media.FontFamily("Segoe UI Semibold")

                };

                ls.LabelOptions = lpo;

                LabelClassCollection LCollection = new LabelClassCollection();

                LCollection.Add(ls);

                LayerDrawingOptions myLayerDrawinOptions = null;

                myLayerDrawinOptions = new LayerDrawingOptions();

                myLayerDrawinOptions.LayerID = myDynamicLayerInfo.ID;

                myLayerDrawinOptions.ShowLabels = p;

                myLayerDrawinOptions.LabelClasses = LCollection;

                LayerDrawingOptionsCollection myLayerDrawingOptionsCollection = null;

                myLayerDrawingOptionsCollection = new LayerDrawingOptionsCollection();

                myLayerDrawingOptionsCollection.Add(myLayerDrawinOptions);

                // Apply the new settings of the Dynamic Layer and refresh the ArcGISDynamicMapServiceLayer.

                flayer.LayerDrawingOptions = myLayerDrawingOptionsCollection;

                flayer.LayerDrawingOptions[0].ShowLabels = p;

                flayer.Refresh();

            }

        }

0 Kudos