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.

FOR FILESYSTEM APP.CONFIG

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

Xamarin.Forms.Forms.Init(); 
ConfigurationManager.Initialise(PCLAppConfig.FileSystemStream.PortableStream.Current); 
LoadApplication(new App()); 

Android (MainActivity.cs)

Xamarin.Forms.Forms.Init(this, bundle);
ConfigurationManager.Initialise(PCLAppConfig.FileSystemStream.PortableStream.Current);
LoadApplication(new App());

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

Xamarin.Forms.Forms.Init(e);
ConfigurationManager.Initialise(PCLAppConfig.FileSystemStream.PortableStream.Current);
LoadApplication(new App());

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

<configuration> 
  <appSettings> 
    <add key="config.text" value="hello from app.settings!" /> 
  </appSettings> 
</configuration>

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’
Access your setting:

ConfigurationManager.AppSettings["config.text"];

FOR EMBEDDED APP.CONFIG

Initialize ConfigurationManager.AppSettings on your pcl project like below:

Assembly assembly = typeof(App).GetTypeInfo().Assembly;
ConfigurationManager.AppSettings = new ConfigurationManager(assembly.GetManifestResourceStream("DemoApp.App.config")).GetAppSettings;

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

Posts

  • ManhaoChenManhaoChen NZMember ✭✭
    edited September 2016

    hi there,

    i've managed to get droid and uwp to work, but unsure where to link the app.config file in ios and what build action is required

    EDITED

    never mind, just saw the demo project, the build action for ios is 'none'

    by default it added as BundleResource

  • MathewPotrykusMathewPotrykus USMember ✭✭

    Hi,

    I tried using you NuGet Package on my Xamarin.Android app and followed the above instructions to try getting it to work however the package includes a bunch of ID Updates into the Resource.Designer.cs file for a bunch of Forms resources that are not included with an Android project. This makes it so that the app con no longer be built due to the missing references and removing the reference to the package that puts them there stops the "PCLAppConfig.FileSystemStream.PortableStream.Current" from being recognised.

    Do you have a NuGet Package that will work for Xamarin.Android projects?

  • DanielBartonDanielBarton USMember

    @MathewPotrykus I was able to get around this issue by updating to the latest version of Xamarin.Forms in each of my projects. This also meant that I had to upgrade to use JDK 1.8 instead of JDK 1.7

  • Angelru9Angelru9 ESMember ✭✭✭
    edited January 2017

    Hi @Xamariners ,

    I put the application in the background and on returning, throw exception

    Is a possible bug?

    01-19 21:47:32.577 E/mono ( 2101): Unhandled Exception:
    01-19 21:47:32.577 E/mono ( 2101): System.TypeInitializationException: The type initializer for 'ConfigurationManager' threw an exception. ---> System.InvalidOperationException: Initialise must be called once in program
    01-19 21:47:32.577 E/mono ( 2101): --- End of inner exception stack trace ---
    01-19 21:47:32.577 E/mono-rt ( 2101): [ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: The type initializer for 'ConfigurationManager' threw an exception. ---> System.InvalidOperationException: Initialise must be called once in program
    01-19 21:47:32.577 E/mono-rt ( 2101): --- End of inner exception stack trace ---

  • AlanDAlanD GBMember

    @Angelru9 No bug, at least not in PCLAppConfig ;) This happens if you have already initialised the configuration manager somewhere else. A.

  • Angelru9Angelru9 ESMember ✭✭✭
    edited February 2017

    @AlanD said:
    @Angelru9 No bug, at least not in PCLAppConfig ;) This happens if you have already initialised the configuration manager somewhere else. A.

    Any solution ? thanks

  • AlanDAlanD GBMember

    Locate the other place you have already initialised the configuration manager.
    Or maybe you are calling the same line of code more than once.
    Put a breakpoint on the configuration manager initialisation code and see if it gets hit more than once.
    A.

  • PhilSeemanPhilSeeman USMember ✭✭

    I've implemented PCLAppConfig in my Xamarin Forms solution. It works great when running the iOS project.

    In running the Android project, when it hits the initialization line

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

    I get the error

    System.NotImplementedException: This functionality is not implemented in the portable version of this assembly. You should reference the PCLStorage NuGet package from your main application project in order to reference the platform-specific implementation.

    I installed PCLAppConfig via Nuget; it installed the PCLStoage dependency references and the PCLAppConfig references in all 3 projects (Portable, iOS, Android).

    Any ideas why iOS is fine but Android is choking?

  • PhilSeemanPhilSeeman USMember ✭✭

    To answer my own question above: following the advice in another thread, I did a Clean and Rebuild on the Android app and that fixed it.

  • DavidJiboyeDavidJiboye NGMember ✭✭

    Also, app.config is not the same as App.config. Case sensitive file name.

  • AnubhabMishra.3872AnubhabMishra.3872 USMember ✭✭

    Hi, I am facing the same issue. But i am unable to understand why would app.xaml.cs call the Initialise function twice. In debug its only calling once but on release it is sometimes crashing with this exception.

    Xamarin caused by: android.runtime.JavaProxyThrowable: System.TypeInitializationException: The type initializer for 'ConfigurationManager' threw an exception. ---> System.InvalidOperationException: Initialise must be called once in program

  • KapilGurnaniKapilGurnani USMember ✭✭

    I got this error:

    "nhandled Exception:
    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'"

    any ideas ?

  • KapilGurnaniKapilGurnani USMember ✭✭

    @DavidJiboye said:
    Also, app.config is not the same as App.config. Case sensitive file name.

    Ya , app.config is already present

  • mariadsgmariadsg ESMember

    Hello:
    I use the PCLAppConfig library to read the config file and it shows me. Now I would like to know what I have to do to be able to modify it in execution, since my file saves the configuration of different objects. That through their reading I show them on screen.
    Thank you

  • Hi

    I am trying PCLAppConfig package in my xamarin application.

    I have created app.config file at PCL solution, it's build action set to EmbeddedResource. PCLAppConfig and PCLStorage package are added to PCL, Android, iOS solution.

    app.config is added to iOS solution with build action "None".
    app.config is added to android solution to Asset folder with build action "AndroidAsset".

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

    In Android, added above line in onCreate() function of MainActivity.cs file
    In iOS , added above line in FinishedLaunching() function of AppDelegate.cs file

    In iOS solution, everything is working correctly.

    But in android, application getting crashed at ConfigurationManager.Initialise(PCLAppConfig.FileSystemStream.PortableStream.Current); with below message.

    Please link the 'App.config' file from your shared pcl project to the 'Assets' directory of your android project, with build action "AndroidAsset"

  • XamarinFileNotFoundXamarinFileNotFound USMember ✭✭
    edited December 2017

    Hi

    I am trying PCLAppConfig package in my xamarin application.

    I have created app.config file at PCL solution, it’s build action set to EmbeddedResource. PCLAppConfig and PCLStorage package are added to PCL, Android, iOS solution.

    App.config is added to iOS solution with build action “None”.
    App.config is added to android solution to Asset folder with build action “AndroidAsset”.

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

    In Android, added above line in onCreate() function of MainActivity.cs file
    In iOS , added above line in FinishedLaunching() function of AppDelegate.cs file

    In iOS solution, everything is working correctly.

    But in android, application getting crashed at ConfigurationManager.Initialise(PCLAppConfig.FileSystemStream.PortableStream.Current); with below message.

    Please link the ‘App.config’ file from your shared pcl project to the ‘Assets’ directory of your android project, with build action “AndroidAsset”

  • Hi @Xamariners

    Please provide solution to above problem.
    Waiting

  • nanomanonanomano USMember ✭✭

    @XamarinFileNotFound, @KapilGurnani

    Folks, did anyone find the solution to this problem? facing the same problem.

  • KapilGurnaniKapilGurnani USMember ✭✭

    @nanomano

    I didn't found any solution, i gave up and use the Application.Current.Properties

    The Application subclass has a static Properties dictionary which can be used to store data, in particular for use in the OnStart, OnSleep, and OnResume methods. This can be accessed from anywhere in your Xamarin.Forms code using Application.Current.Properties.

  • nanomanonanomano USMember ✭✭

    Thanks @KapilGurnani for getting back to me, for me it was a noob mistake and i was missing some keys in the app.config file for UAT env. and VS error was kind of misleading also.

Sign In or Register to comment.