When working with Xamarin, there's a very common issue developers run into early on. Where's the App.config? Or, where's the appsettings.json? Short answer, there isn't one. Why? I have no idea. It seems obvious to include such a well understood configuration file in the Xamarin.Forms and Xamarin Native platform, but alas, the Xamarin team have not done so.

But, there are options. I have reviewed a few of the configuration file options myself but there's one that I settled on purely for its simplicity. The library is called PCLAppConfig and can be found here. And, despite its name, this library does use .Net Standard. You can find the Nuget package here.

What I like about this solution is the ability to create a configuration file and just use it as an embedded resource in the core project. Simply add your configuration file to your core project, make sure you mark its build action as an embedded resource and load it up when your app starts with the following:

Assembly assembly = typeof(App).GetTypeInfo().Assembly;
	ConfigurationManager.Initialise(assembly.GetManifestResourceStream("DemoApp.App.config"));

Make sure you fully qualify the name of the configuration file with your assembly name. So, if your app is called WallysWidgetApp, you would access the embedded resource using the path "WallysWidgetApp.App.config" assuming your configuration file named "App.config" is at the root of your project structure.

All you need now is a configuration file that looks like the old-school .Net App.config. Use something like this:

<configuration>
	<appSettings>
        <add key="WallyAPIKey" value="somecrazylongencryptedstringandstuff" />
    </appSettings>
</configuration>

Then, you can access your configuration like this:

ConfigurationManager.AppSettings["WallyAPIKey"];

But, you really don't want to litter your code-base with calls to this configuration manager. That's ugly and ties you to this particular implementation. Instead, it's better to abstract your configuration into its own injectable type. This way, if you come across a better library in the future, you're not revisiting the entire app to find every reference to this configuration manager.

A configuration type can look something like this as an example:

using System;
using PCLAppConfig;

namespace WallysWidgetApp
{
    public class AppConfig : IAppConfig
    {
        public string WallyAPIKey
        {
            get { return ConfigurationManager.AppSettings["WallyAPIKey"]; }
        }
    }

    public interface IAppConfig
    {
        string WallyAPIKey { get; }
    }
}

Now you can use this class to access your configuration and you can inject it into your view models. Of course, there are many ways to implement configuration in Xamarin. This happens to be an easy way that works pretty well.