I want to store development vs production connection strings and configuration strings in a monodroid project. I would normally store it as app settings in a web.config or an app.config, but how should I do it in monodroid and monotouch projects? I would also like for it to switch configurations automatically between debug and release builds just as visual studio does with *.config files. In an iOS app I could store these in a plist but I'd like a cross platform solution in mono.
How would I do this in monodroid or monotouch?
To access these values, there is one static class named ConfigurationManager, which has one getter property named AppSettings. We can just pass the key inside the AppSettings and get the desired value from AppSettings section, as shown below.
ConfigurationManager is the class which helps to read data from configurations. Provides access to configuration files for client applications. To use the ConfigurationManager class, your project must reference the System. Configuration assembly.
You can mark the settings as public in the C# project ("Access Modifier" in the property pane of settings) and then you can access it from the vb project (don't forget to add the reference).
ConfigurationManager was added to support ASP.NET Core's new WebApplication model, used for simplifying the ASP.NET Core startup code.
You should just use a static class with #if
declarations.
Something like:
public static class Configuration {
#if DEBUG
public const string ConnectionString = "debug string";
#else
public const string ConnectionString = "release string";
#endif
}
The benefit to using app.config
is the ability to change these settings on the file system without recompiling. On mobile, there isn't a good way (especially on iOS) to edit the file after it's deployed. So it's generally better to just use a static class and redeploy when you need to change the values. This will also work on all platforms, because it is just C# code doing the work.
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"];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With