Monodroid: where should I put the configuration settings?

From Miguel de Icaza :

We use a library profile that is better suited for mobile devices, so we removed unnecessary features (for example, the entire System.Configuration stack, like Silverlight).

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

  • When using Mono for Android, where should I put my configuration settings?
    • If that matters, I would like to save different configuration options for different build configurations.
+7
source share
4 answers

I would recommend using general settings and compilation symbols to manage various configurations. The following is an example of how you can use the settings file to add or change keys based on compilation characters. In addition, you can create a separate settings file, available only for a specific configuration. Since these keys are not available for all configurations, always check before using them.

 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(); 
+11
source

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

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

And use it with the following 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); } } } 

through a singleton class, we call Configuration, so for .NET developers this is very similar to using app.config files. This may not be the most effective solution, but it is doing its job.

+3
source

ITNOA

Perhaps PCLAppConfig will help you create and read from app.config in your Xamarin.Forms PCL project or other Xamarin projects.

For different configurations in different build modes, such as release and debugging, you can use Configuration Transform on app.config .

0
source

there is a Xamarin Centric AppSetting reader: https://www.nuget.org/packages/PCLAppConfig is quite useful for continuous delivery (therefore, a deployment server such as an octopus allows you to modify the configuration file for each environment with values ​​stored on the cd server).

there is a built-in Xamarin reader AppSetting at https://www.nuget.org/packages/PCLAppConfig this is very useful for continuous delivery;

use below:

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

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

3) Initialize ConfigurationManager.AppSettings in each of your platform projects right after the Xamarin.Forms.Forms.Init instruction, which is located on AppDelegate in iOS, MainActivity.cs in Android, application in UWP / Windows 8.1 / WP 8.1:

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

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

0
source

All Articles