Monodroid: Where should I put configuration settings?

前端 未结 4 659
清酒与你
清酒与你 2020-12-16 22:06

From Miguel de Icaza:

We use a library profile that is better suited for mobile devices, so we removed features that are not necessary (like the entir

4条回答
  •  爱一瞬间的悲伤
    2020-12-16 22:38

    We have had exactly the same problem in our current project. My first impulse was to put the configuration in a sqlite key-value table but then my internal customer reminded me the main reason for a configuration file - it should support simple editing.
    So instead we created an XML file and put it there:

    string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
    

    And access it using these properties:

    public string this[string key]
        {
            get
            {
                var document = XDocument.Load(ConfigurationFilePath);
                var values = from n in document.Root.Elements()
                           where n.Name == key
                           select n.Value;
                if(values.Any())
                {
                    return values.First();
                }
                return null;
            }
            set
            {
                var document = XDocument.Load(ConfigurationFilePath);
                var values = from n in document.Root.Elements()
                           where n.Name == key
                           select n;
                if(values.Any())
                {
                    values.First().Value = value;
                }
                else
                {
                    document.Root.Add(new XElement(key, value));
                }
                document.Save(ConfigurationFilePath);
            }
        }
    }
    

    via a singleton class we call Configuration so for .NET developers it is very similar to using the app.config files. Might not be the most efficient solution but it gets the job done.

提交回复
热议问题