Blog

AppSettings Reader for Xamarin and Xamarin.Forms

Reading app.config files in a Xamarin.Forms Xaml project

While each mobile platforms do offer their own settings management api, there are no built in ways to read settings from a good old .net style app.config xml file; This is due to a bunch of good reasons reasons, notably the .net framework configuration management api being on the heavyweight side, and each platform having their own file system api.

So we built a simple PCLAppConfig library nicely nuget packaged for your immediate consumption.

This library makes use of the lovely PCLStorage library

This example assumes you are developing a Xamarin.Forms Xaml project, where you would need to access settings from your shared viewmodel.

You need to add the PCLAppConfig library to your pcl and all target projects.

FOR FILESYSTEM APP.CONFIG

  1. Initialize ConfigurationManager.AppSettings on each of your platform project, just after the ‘Xamarin.Forms.Forms.Init’ statement, as per below:

iOS (AppDelegate.cs)

Android (MainActivity.cs)

UWP / Windows 8.1 / WP 8.1 (App.xaml.cs)

  1. Add an app.config file to your shared PCL project, and add your appSettings entries, as you would do with any app.config file

  1. Add this PCL app.config file 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’
  2. Access your setting:

FOR EMBEDDED APP.CONFIG

  1. Initialize ConfigurationManager.AppSettings on your pcl project like below:
  2. Add an app.config on your shared pcl project and ensure that Build Action:EmbeddedResource, and add your appSettings entries, as you would do with any app.config

The source code and demo app are available in github

8 thoughts on “AppSettings Reader for Xamarin and Xamarin.Forms

  1. Juan says:

    Hi ben this is a nice post. At least I can find an alternate to CrossSettings plugin from jamesmontemagno…

    May I suggest to elaborate more on how to use MVVM properly especially if we deal with frameworks such as mvvmcross or mvvm light?

    1. ben says:

      Hi Juan,
      There are quite a few posts out there that already explains how to do MVVM with Xamarin and Xamarin.Forms, with or additional MVVM framework layer:

      Basic MVVM (using commands) : http://michaelcrump.net/use-mvvm-with-xamarin-forms/

      MVVM with Prism: https://channel9.msdn.com/Events/Xamarin-Evolve/2016/MVVM-Made-Simple-with-XamarinForms-and-Prism

      MVVM with XLAbs: http://www.codenutz.com/getting-started-xamarin-forms-labs-xaml-mvvm-ioc/

      MVVM with MVVMCross: http://jonathanroux.com/2016/05/05/your-first-project-with-xamarin-and-mvvmcross-4/

      MVVM with MVVMLight http://www.c-sharpcorner.com/UploadFile/7078b8/getting-started-with-xamarin-forms-mvvm-light/

      MVVM with Fresh MVVM https://github.com/rid00z/FreshMvvm

      Is there something specific that you see is missing that I can help clarify?

  2. Juan says:

    Hi Ben,

    I think this one is already quite enough.. Perhaps i am gonna start from the very basic one
    http://michaelcrump.net/use-mvvm-with-xamarin-forms/

    1. ben says:

      great. let me know if you need further guidance

  3. Juan says:

    Hi Ben,

    If you ever got a chance or maybe if u ever used this add-in: XAML previewer ?
    https://developer.xamarin.com/guides/xamarin-forms/xaml/xaml-previewer/

    I already switched my xamarin studio to alpha channel but still i am not able to preview my XAML..

  4. Massimo says:

    In android I added the link file app.config to the Assets folder, setted AndroidAsset in build config, but I’ve System.IO.FileNotFoundException: please link the ‘App.config’ file from your shared pcl project to the ‘Assets’ directory of your android project, with build action ‘AndroidAsset’.
    I’m sure the file exist because is readable with:
    AssetManager assets = this.Assets;
    using (StreamReader sr = new StreamReader (assets.Open (“app.config”)))
    {
    content = sr.ReadToEnd ();
    }

    1. ben says:

      Did you add the nuget to your PCL and target projects?
      check out the demo app in doubt: https://github.com/mrbrl/PCLAppConfig
      if you still cannot resolve the issue, please send me a sample project that reproduces the issue.

      1. Massimo says:

        I recreate the project and now works. Many thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *