Custom Tasks and Accessing Settings from AMP file?

444
5
07-27-2011 09:04 PM
PeterTimmers
Occasional Contributor III
Has anyone done that? 

Is there anyway of accessing the amp file setting through the code?  I've looked ... but haven't found anything yet.  The update Synchronize task obviously gets settings from the Amp file (see below).

<Task assemblyQualifiedName="CustomizationSamples.CustomCollectFeaturesTask, CustomCollectFeaturesTask, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
      <CustomCollectFeaturesTask name="Collect Feature" description="Custom Collect Features Task" />
    </Task>



<Task assemblyQualifiedName="ESRI.ArcGIS.Mobile.Client.Tasks.Synchronization.SynchronizeTask, ESRI.ArcGIS.Mobile.Client">
      <SynchronizeTask xmlns:amp="http://www.esri.com/schemas/ArcGIS/Mobile/1" name="Manage Edits" description="View and manage local updates">
        <PostUpdatesSettings autoPostOption="Never">
          <TimeInterval minutes="-1" hours="1" />
        </PostUpdatesSettings>
        <GetDataSettings>
          <Layers />
        </GetDataSettings>
      </SynchronizeTask>
0 Kudos
5 Replies
AkhilParujanwala
New Contributor III
I have tried to access the amp (xml) settings using C#. But I can't do it. So instead I can call the upon the Settings page using code, and have the user change it manually.

For example, I will be programming a procedure to check the Tracking Settings, if tracking = off, then show Tracking Settings page to allow the user to click on, then continue with procedure.

I believe you will have to do the same, unless you can figure out a way to change those xml variables in C#. In my case the Tracking settings is a GET only object, meaning I can't set it in code. Thus making the user click on the Tracking Settings page to turn it off manually.
0 Kudos
PeterTimmers
Occasional Contributor III
Seems logical though doesn't it.  And since the synchronization task can do it, you'd think it must be in the structure...

More doco please ESRI.

I want to reuse a custom collect feature task with different settings a number of times in a single project.  I suppose I can setup a workflow with multiple pages... but to code what I was thinking of would be simpler.

Or another idea -- say you've written a project for a specific event and you want to tag all that data collected with the event name... having it in the amp file and prefilling that value on a collect feature would be good!!

I'm hoping someone at esri inc is listening.
0 Kudos
JonathanCarewick
New Contributor
hey guys, I have two solutions for you I believe that I've come across. First, any setting that's currently already in the ApplictionSettings.xml should be accessible through the current mobile application's settings. I don't think you have to navigate the user to the settings page?

For instance, on mobile side for coding, you can do the following:
MobileApplication.Current.Settings.OpenGpsOnStartup = true;


Does this not work for you? It seems to work for me. If not, you may want to try this:

I actually successfully saved changes to the settings file in code a different way. However, in my case it was a new entry altogether (new attribute) that I wanted to add. I had to use C# code and none of the built in ESRI environment code. However, I did this on the mobile side. Also, I must note that I did it with success, but once you closed ArcGIS Mobile and open it again, ArcGIS Mobile overwrites the Settings XML file getting rid of my custom attribute... So I ended up creating my own custom XML file anyways. But maybe this will be of help to someone to change current attributes?

First I got the location of the DLL being used to get the location of where the settings xml file is:
strDllFullPath = System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName;


Next, I had the location of the XML file as something like "\SD-MMC card\Program Files\ArcGIS Mobile\10\ApplicationSettings.xml" so I could then load the XML file into an XMLDocument, then changed the XML document in C# code (Using System.XML)
XmlDocument settingsXMLDoc = new XMLDocument();
settingsXMLDoc.Load(strDllFullPath);

// Etc, Etc.



When my XMLDocument was all good to go, I then used the System.IO.File.Delete method to delete the settings file, then used the objXmlDocument.Save("\SD-MMC card\Program Files\ArcGIS Mobile\10\ApplicationSettings.xml") method to replace the XML document. This was okay because the settings file was already loaded into my XMLDocument object, all I needed to do was write the contents to file using the save method.

I would imagine that any setting that's available already through the API (such as my first code example), would not get striped out by arcgis mobile on startup.

I also posted this piece of code for anyone looking to edit "ApplicationSettings.xml" since it's NOT possible to add your own attributes or nodes... Found that out the hard way unless someone else knows better.
0 Kudos
StephenDickinson
Esri Contributor
Has anyone done that? 

Is there anyway of accessing the amp file setting through the code?


Does this help? It's for a custom extension, but should transfer to a custom task ok.  I've included code for the Project Center extension, project extension and shared settings classs.

Note: You must serialize out exactly the same settings that are deserialized in, otherwise the AMP file will become unreadable.

"TestExtension_ProjectCenter" Class Library Project -> TestExtension_ProjectCenter.xaml.cs

    public partial class TestExtension : UserControl, IProjectExtension
    {
        // TODO: Set default name and description for your project extension
        private string m_displayName = "A Test Extension";
        private string m_description = "Test Task Extension";
        private Settings m_testExtensionSettings = new Settings();

 ...

        #region IXmlSerializable Members

        /// <summary>
        /// This method is reserved and should not be used. When implementing the IXmlSerializable interface, you should return null (Nothing in Visual Basic) from this method, and instead, if specifying a custom schema is required, apply the <see cref="T:System.Xml.Serialization.XmlSchemaProviderAttribute"/> to the class.
        /// </summary>
        /// <returns>
        /// An <see cref="T:System.Xml.Schema.XmlSchema"/> that describes the XML representation of the object that is produced by the <see cref="M:System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)"/> method and consumed by the <see cref="M:System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)"/> method.
        /// </returns>
        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        /// <summary>
        /// Generates an object from its XML representation.
        /// TODO: If you change your Project Center class name, replace 'TestExtension_ProjectCenter' 
        /// with the new class name
        /// </summary>
        /// <param name="reader">The <see cref="T:System.Xml.XmlReader"/> stream from which the object is deserialized.</param>
        public void ReadXml(System.Xml.XmlReader reader)
        {
            string name = reader.GetAttribute("name");
            if (name != null)
                DisplayName = name;

            string desc = reader.GetAttribute("description");
            if (desc != null)
                Description = desc;

            if (reader.IsEmptyElement)
            {
                reader.Read();
                return;
            }

            reader.Read();

            while (true)
            {
                if (reader.Name == "Settings")
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                    m_testExtensionSettings = (Settings)serializer.Deserialize(reader);
                }
                else if (reader.NodeType == XmlNodeType.EndElement)
                {
                    reader.Read();
                    break;
                }
                else if (reader.EOF)
                {
                    break;
                }
                else
                {
                    // read over possible unknown element
                    reader.Skip();
                }
            }
        }

        /// <summary>
        /// Converts an object into its XML representation.
        /// </summary>
        /// <param name="writer">The <see cref="T:System.Xml.XmlWriter"/> stream to which the object is serialized.</param>
        public void WriteXml(System.Xml.XmlWriter writer)
        {
            // Writes basic assembly information to arcgis mobile project configuration file
            writer.WriteAttributeString("name", DisplayName);
            writer.WriteAttributeString("description", Description);

            // TODO: Writes additional elements if need be
            XmlSerializer serializer = new XmlSerializer(typeof(Settings));
            serializer.Serialize(writer, m_testExtensionSettings);
        }
 
 ...

    }


"TestExtension_Win" Class Library Project -> TestExtension_Win.cs

    public class TestExtension : ProjectExtension
    {
        private string m_displayName;
        private string m_description;
        private Assembly m_assembly = Assembly.GetExecutingAssembly();
        private Settings m_testExtensionSettings;

        /// <summary>
        /// Gets/sets Description for your capability
        /// </summary>
        public string Description
        {
            get { return m_description; }
            set { m_description = value; }
        }

        /// <summary>
        /// Gets/sets DisplayName for your capability
        /// </summary>
        public string DisplayName
        {
            get { return m_displayName; }
            set { m_displayName = value; }
        }

        protected override void Initialize()
        {
            // TODO:
            // Note: This method is called by the Project when it's initializing. At this point
            // all other Tasks and ProjectExtensions have been instantiated.

            string message = String.Format("{0} Initialized", m_assembly.FullName);
            ESRI.ArcGIS.Mobile.Client.Windows.MessageBox.ShowDialog(message, m_assembly.GetName().Name);
        }

        protected override void OnOwnerInitialized()
        {
            // TODO:
            // Note: This method is called by the Project when it's Initialized. At this point
            // all other Tasks and ProjectExtensions will have been Initialized
            // Do not add/remove ProjectExtensions to the Project or Application during this method.
        }

        protected override void Uninitialize()
        {
            // TODO: Dispose or Detach events from application level objects
            // Note: This method is called by the Project before the Project is closed.

            string message = String.Format("{0} Uninitialized", m_assembly.FullName);
            ESRI.ArcGIS.Mobile.Client.Windows.MessageBox.ShowDialog(message, m_assembly.GetName().Name);
        }

        public override void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteAttributeString("name", DisplayName);
            writer.WriteAttributeString("description", Description);

            XmlSerializer serializer = new XmlSerializer(typeof(Settings));
            serializer.Serialize(writer, m_testExtensionSettings);
        }

        public override void ReadXml(System.Xml.XmlReader reader)
        {
            string name = reader.GetAttribute("name");
            if (name != null)
                DisplayName = name;

            string desc = reader.GetAttribute("description");
            if (desc != null)
                Description = desc;

            if (reader.IsEmptyElement)
            {
                reader.Read();
                return;
            }

            reader.Read();

            while (true)
            {
                if (reader.Name == "Settings")
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                    m_testExtensionSettings = (Settings)serializer.Deserialize(reader);
                }
                else if (reader.NodeType == XmlNodeType.EndElement)
                {
                    reader.Read();
                    break;
                }
                else if (reader.EOF)
                {
                    break;
                }
                else
                {
                    // read over possible unknown element
                    reader.Skip();
                }
            }
        }
    }


"TestExtension_Common" Class Library Project -> Settings.cs

    public class Settings
    {
        private string m_testSetting ="Default Value";

        public string TestSetting
        {
            get
            {
                return m_testSetting;
            }
            set
            {
                m_testSetting = value;
            }
        }
    }
0 Kudos
JoshWerts
New Contributor III
Steve's post was very helpful and worked great with one extension. However, I'm having trouble with multiple extensions reading and writing from the amp file.

With 2 extensions, the first time I load the project (Windows Mobile), ext 1 and ext 2 both call the readXml method and everything's good.  However, when closing, only ext 1's WriteXml method gets called.

Upon opening the project again, Only ext 1's ReadXml method gets called.  However, upon exiting, both extension's WriteXml method gets called, and then when opened again, both ReadXml's get called and so on.  However, at this point since the ext 2's WriteXml did not get called on the first close, that extension's settings are lost.

This pattern continues if you keep opening and closing the project.

Anyone have any idea on what's up with this behavior?

Thanks!
Josh

stephendickinson;121542 wrote:
Does this help? It's for a custom extension, but should transfer to a custom task ok.  I've included code for the Project Center extension, project extension and shared settings classs.


Note: You must serialize out exactly the same settings that are deserialized in, otherwise the AMP file will become unreadable.

...
0 Kudos