Monodroid: Where should I put configuration settings?

旧时模样 提交于 2019-12-18 04:09:29

问题


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 entire System.Configuration stack, just like Silverlight does).

After years of .NET development, I'm accustomed to storing configuration settings in web.config and app.config files.

  • When using Mono for Android, where should I put my configuration settings?
    • If it matters, I'd like to store different configuration settings for different build configurations as well.

回答1:


I would probably recommend using shared preferences and compilation symbols to manage different configurations. Below is an example of how you can use a preferences file to add or change keys based on the compilation symbols. Additionally, you could create a separate preferences file that is only available for a particular configuration. Because these keys are not available on all configurations, make sure to always perform checks for them before using.

var prefs = this.GetSharedPreferences("Config File Name", FileCreationMode.Private);
var editor = prefs.Edit();

#if MonoRelease
editor.PutString("MyKey", "My Release Value");
editor.PutString("ReleaseKey", "My Release Value");
#else
editor.PutString("MyKey", "My Debug Value");
editor.PutString("DebugKey", "My Debug Value");
#endif

editor.PutString("CommonKey", "Common Value");
editor.Commit();



回答2:


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.




回答3:


there's a Xamarin centric AppSetting reader: https://www.nuget.org/packages/PCLAppConfig pretty useful for continuous delivery (so a deployment server such as octopus allows to alter your config file for each environment with values stored on the cd server)

there's a Xamarin centric AppSetting reader available at https://www.nuget.org/packages/PCLAppConfig it is pretty useful for continuous delivery;

use as per below:

1) Add the nuget package reference to your pcl and platforms projects.

2) Add a app.config file on your PCL project, then as a linked file on all your platform projects. For android, make sure to set the build action to 'AndroidAsset', for UWP set the build action to 'Content'. Add you settings keys/values: <add key="config.text" value="hello from app.settings!" />

3) Initialize the ConfigurationManager.AppSettings on each of your platform project, just after the 'Xamarin.Forms.Forms.Init' statement, that's on AppDelegate in iOS, MainActivity.cs in Android, App in UWP/Windows 8.1/WP 8.1:

ConfigurationManager.Initialise(PCLAppConfig.FileSystemStream.PortableStream.Current);

3) Read your settings : ConfigurationManager.AppSettings["config.text"];




回答4:


ITNOA

Maybe PCLAppConfig is help you to create and read from app.config in Xamarin.Forms PCL Project or other Xamarin projects.

For different configuration in different build mode such as release and debug you can use Configuration Transform on app.config.



来源:https://stackoverflow.com/questions/14401008/monodroid-where-should-i-put-configuration-settings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!