/// <summary>
        /// This function is used to apply a raster function template onto an image service layer.
        /// </summary>
        /// <param name="selectedLayer">The selected image service layer. </param>
        /// <param name="operation">An Enum type represents the user selected operation. </param>
        /// <param name="xmlFilePath">The XML file path.</param>
        /// <returns></returns>
        public static async Task ApplyRFTXmlFile(ImageServiceLayer selectedLayer, Enum operation, string xmlFilePath)
        {
            // Creates a new instance of XmlDocument class.
            XmlDocument xmlDoc = new XmlDocument();

            // Loads the RFT XML file from its file path.
            xmlDoc.Load(xmlFilePath);

            // Creates a new instance of the rendering rule.
            CIMRenderingRule setRenderingrule = new CIMRenderingRule();

            // Gets the markup containing all the nodes and their child nodes, passes to the rendering rule definition.
            setRenderingrule.Definition = xmlDoc.OuterXml;

            // Defines the rendering rule name as the operation name.
            setRenderingrule.Name = Convert.ToString(operation);

            await QueuedTask.Run(() =>
            {
                // Sets the new rendering rule to the selected layer.
                selectedLayer.SetRenderingRule(setRenderingrule);

                // Gets the current rendering rule from the layer.
                CIMRenderingRule renderingRule = selectedLayer.GetRenderingRule();

                //Verifies if the current rendering rule name is correct. Else, error message shows up.
                if (renderingRule.Name != Convert.ToString(operation))
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("The operation is not succeeded, please check...",
                                                                     "Operation unsucceeded:", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            });
        }
        /// <summary>
        /// Called when the combo box item selection changed.
        /// </summary>
        /// <param name="item"> The selected combo box item. </param>
        protected override async void OnSelectionChange(ComboBoxItem item)
        {
            // Sets logic if selected combo box item is null, then return.
            if (item == null)
            {
                return;
            }

            // Passes the current selected combo box item to the selectedComboBoxItem.
            selectedComboBoxItem = item;

            // Adds validation here if the item text is emply then return.
            if (string.IsNullOrEmpty(item.Text))
            {
                return;
            }

            // Try and get the first selected layer.
            Layer firstSelectedLayer = null;

            try { firstSelectedLayer = MapView.Active.GetSelectedLayers().First(); } catch (Exception) { }
            // Check if there are any selected layers and if the first selected layer is a image service layer.
            if (!(firstSelectedLayer != null && firstSelectedLayer is ImageServiceLayer))
            {
                MessageBox.Show("Please select an image service layer.");
                return;
            }
            ImageServiceLayer selectedLayer = firstSelectedLayer as ImageServiceLayer;

            // Enters if the selected combo box item is not 'defult' or empty, else sets the original rendering rule to the selected layer.
            if (item.Text != "None" && item.Text != null)
            {
                // Gets the operation enum item from the its name.
                CellStatistics_Operations operation = (CellStatistics_Operations)Enum.Parse(typeof(CellStatistics_Operations), item.Text);
                try
                {
                    string rftFilePath = Project.Current.HomeFolderPath + fileRelativePath;
                    // Customizes the raster function template XML file using the user defined definition query and operation.
                    string xmlFilePath = Process.CustomRFTXmlFile(rftFilePath, operation, DefQueryEditBox.passingText);
                    // Applies the custom raster function template to the selected layer.
                    await Process.ApplyRFTXmlFile(selectedLayer, operation, xmlFilePath);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Exception caught in OnSelectionChange:" + ex.Message, "Exception", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else
            {
                await QueuedTask.Run(() =>
                {
                    // Sets the defult rendering rule to the selected image service layer.
                    selectedLayer.SetRenderingRule(renderingRule_default);
                });
            }
        }
        /// <summary>
        /// Set the processing template on the first selected image service layer in the first open 2D map.
        /// </summary>
        /// <param name="templateName">The name of the processing template to set on the layer.</param>
        /// <returns></returns>
        public static async Task SetProcessingTemplateAsync(string templateName)
        {
            try
            {
                // Get the first 2D map from the project that is called Map.
                Map _map = await GetMapFromProject(Project.Current, "Map");

                if (_map == null)
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("SetResamplingType: Failed to get map.");
                    return;
                }

                // Get the most recently selected layer.
                Layer firstSelectedLayer = MapView.Active.GetSelectedLayers().First();
                // Check if the first selected layer is an image service layer.
                if (firstSelectedLayer is ImageServiceLayer)
                {
                    // Set the colorizer on the most recently selected layer.
                    // The colorizer has to be set on the Main CIM Thread (MCT).
                    ImageServiceLayer isLayer = (ImageServiceLayer)firstSelectedLayer;
                    await QueuedTask.Run(() =>
                    {
                        // Create a new Rendering rule
                        CIMRenderingRule setRenderingrule = new CIMRenderingRule()
                        {
                            // Set the name of the rendering rule.
                            Name = templateName
                        };
                        // Update the image service with the new mosaic rule.
                        isLayer.SetRenderingRule(setRenderingrule);
                    });
                }
            }
            catch (Exception exc)
            {
                // Catch any exception found and display a message box.
                ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Exception caught while trying to set processing template: " + exc.Message);
                return;
            }
        }
        // Event Handler, gets name of selected RFT and changes the rendering rule of the Image Service to be displayed accordingly
        private async void RFT_SelectChanged(object sender)
        {
            try
            {
                //Map _map1 = await GetMapFromProject(Project.Current, "Map");
                Map _map = MapView.Active.Map;
                if (_map == null)
                {
                    ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("AddRasterLayerToMap: Failed to get map.");
                    return;
                }

                string            dataSoureUrl      = ServiceURL;
                ImageServiceLayer imageServiceLayer = null;
                await QueuedTask.Run(() =>
                {
                    imageServiceLayer = (ImageServiceLayer)LayerFactory.Instance.CreateLayer(new Uri(dataSoureUrl), _map);
                    if (imageServiceLayer == null)
                    {
                        ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Failed to create layer for url:" + dataSoureUrl);
                        return;
                    }
                    ImageServiceLayer isLayer = imageServiceLayer;
                    // Create a new Rendering rule
                    CIMRenderingRule setRenderingrule = new CIMRenderingRule();
                    // Set the name of the rendering rule.
                    setRenderingrule.Name = sender.ToString();
                    // Update the image service with the new mosaic rule.
                    isLayer.SetRenderingRule(setRenderingrule);
                    string originalLayerName = isLayer.Name;
                    isLayer.SetName(originalLayerName + "_" + sender.ToString());
                });
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message);
            }
        }