Monodroid: Where should I put configuration settin

2019-01-24 11:14发布

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.

4条回答
霸刀☆藐视天下
2楼-- · 2019-01-24 11:32

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"];

查看更多
混吃等死
3楼-- · 2019-01-24 11:34

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.

查看更多
来,给爷笑一个
4楼-- · 2019-01-24 11:44

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.

查看更多
放我归山
5楼-- · 2019-01-24 11:52

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();
查看更多
登录 后发表回答