// Initializes the popup title on a graphics layer, given the title field name
        private void setPopupTitle(string titleField, GraphicsLayer layer)
        {
            Dictionary <int, string> expression = new Dictionary <int, string>();

            expression.Add(-1, string.Format("{{{0}}}", titleField));
            LayerProperties.SetPopupTitleExpressions(Target, expression);
        }
        protected override void Invoke(object parameter)
        {
            // Make sure the target is a GraphicsLayer
            if (Target != null && Target is GraphicsLayer)
            {
                // Check whether a title expression has been defined
                if (string.IsNullOrEmpty(TitleExpression))
                {
                    // No title expression, so auto-initialize popups

                    // If the target layer is a feature layer, check whether the service metadata specifies a display field
                    if (Target is FeatureLayer)
                    {
                        FeatureLayer fLayer = (FeatureLayer)Target;

                        // Check if the layer's metadata defines a display field
                        if (fLayer.LayerInfo != null && !string.IsNullOrEmpty(fLayer.LayerInfo.DisplayField))
                        {
                            // use the display field
                            setPopupTitle(fLayer.LayerInfo.DisplayField, fLayer);
                            return;
                        }
                        else if (!fLayer.IsInitialized)
                        {
                            // Wait for the layer to initialize and check again
                            fLayer.Initialized += Layer_Initialized;
                            return;
                        }
                    }

                    // The layer is not a feature layer (i.e. does not have metadata), so auto-determine the display field
                    // based on the graphics in the layer
                    initPopupTitleFromGraphics((GraphicsLayer)Target);
                }
                else
                {
                    // A title expression has been explicitly defined, so use that
                    Dictionary <int, string> expression = new Dictionary <int, string>();
                    expression.Add(-1, TitleExpression);
                    LayerProperties.SetPopupTitleExpressions(Target, expression);
                }
            }
        }
        /// <summary>
        /// Sets the title of the pop-up
        /// </summary>
        /// <remarks>
        /// In cases where the pop-up title is not set automatically (and therefore results in a blank title),
        /// this method loops through the fields in the layer to determine an appropriate title to display.
        /// </remarks>
        private void SetPopupTitleExpression()
        {
            // Dictionary with integer keys and string values. Integer keys are layer ids: -1 for a GraphicLayer,
            // otherwise Layer ID. String values are format strings like this: "{NAME}: {POPULATION}"
            IDictionary <int, string> titleExpression = new Dictionary <int, string>();

            // Check that the LayerInfo and DisplayField are not null. Use the DisplayField if it exists.
            if (resultsLayer.LayerInfo != null && resultsLayer.LayerInfo.DisplayField != null)
            {
                string layerInfoDisplayField  = resultsLayer.LayerInfo.DisplayField;
                string displayFieldExpression = "{" + layerInfoDisplayField + "}";

                titleExpression.Add(-1, displayFieldExpression);

                LayerProperties.SetPopupTitleExpressions(resultsLayer, titleExpression);
            }
            else // display field is null
            {
                // Create a list of all the fields in the layer
                List <Field> fields = resultsLayer.LayerInfo.Fields;

                // Loop through each field to see if there is a FieldName called "Name"(case-insensitive).
                foreach (Field field in fields)
                {
                    string fieldName = field.FieldName;

                    if (fieldName != null)
                    {
                        if (string.Compare(fieldName, "Name", StringComparison.InvariantCultureIgnoreCase) == 0)
                        {
                            titleExpression.Add(-1, "{" + fieldName + "}");
                            LayerProperties.SetPopupTitleExpressions(resultsLayer, titleExpression);
                            return; // If the Popup title expression is set, exit.
                        }
                    }
                }

                // Loop through each field to see if there is a FieldName that contains "Name" (case-insensitive).
                foreach (Field field in fields)
                {
                    string fieldName = field.FieldName;

                    if (fieldName != null)
                    {
                        // if index is -1 then the FieldName does not contain "Name" (case-insensitive)
                        int index = fieldName.IndexOf("Name", StringComparison.InvariantCultureIgnoreCase);

                        if (index >= 0)
                        {
                            titleExpression.Add(-1, "{" + fieldName + "}");
                            LayerProperties.SetPopupTitleExpressions(resultsLayer, titleExpression);
                            return; // If the Popup title expression is set, exit.
                        }
                    }
                }

                // Loop through each field and look for the first field that is both visible in the pop-up attribute list and is a string
                foreach (Field field in fields)
                {
                    string fieldName = field.FieldName;

                    foreach (FieldSettings fSetting in popupInfo.PopupItem.FieldInfos)
                    {
                        if (fieldName != null && fieldName == fSetting.Name && fSetting.FieldType == FieldType.Text)
                        {
                            titleExpression.Add(-1, "{" + fieldName + "}");
                            LayerProperties.SetPopupTitleExpressions(resultsLayer, titleExpression);
                        }
                        return;
                    }
                }
            }
        }