Xamarin.Forms Using Configuration Files

Xamarin.Forms Using Configuration Files

One things I have really appreciated as part of .Net projects is the configuration file which has allowed me to centralize settings and mutable values that I do not want scattered through my code. The ability for these configurations to be set based on environment or compilation state is also a must since values are determine based on deployment.

You can image my frustration that Xamarin projects do not have a default configuration file similar to web or windows applications. Well, this just won’t do and as a result, here is an implementation that might make your life a bit easier.

We first begin with creating a static class that read files that are set as embedded resources.

The next thing we need to do is create POCO objects that represent configuration settings we want to capture and set for the application. I started this process in reverse by creating my json file first with fake data and then going to json2csharp.com and having the site create the classes. I then pasted the class definitions into a single file called ConfigurationModels. I did this to provide a separation of concerns since this is an evolving process and I will probably being changing things in the future.

Once we have created the Models, we just need to create a singleton classes that loads the embedded resource file, parse it and read the values into public properties that can be accessed by the application.

You can see the entire working example on github. Download the reference guide project and CommonCore.






Hey Les thanks for introducing me to xamarin I'm going to be publishing my first app to the play store very soon.

Like
Reply

To view or add a comment, sign in

More articles by Les Brown

  • Accessibility in Xamarin.Forms

    In the last American census, it was estimated that 18.7% of the population has some type of disability and of those…

  • Docker Developer

    An Overview When talking about software containers it is helpful to start with its conceptual cousin known as…

    1 Comment
  • Speedup your development with Adobe XD

    If you have been developing mobile applications for any length of time, you have experienced the headache of creating…

    1 Comment
  • Globalization Alternatives in Xamarin.Forms

    Like all applications that are utilized across disparate cultures and languages, mobile has to be designed with…

  • Xamarin.Forms Behaviors and Commands UI Validation.

    No matter how much we try to ensure data integrity with business rules at various levels of the application, the UX…

  • Xamarin.Forms’ DependencyService as a full-fledged IOC.

    In a previously published article I touted the benefits of using Microsoft’s Unity library for dependency injection and…

    3 Comments
  • Xamarin.Forms & Microsoft Unity 4.0.1

    Xamarin.Forms has built-in features for dependency injection but there are gaps.

  • Xamarin - Custom Fonts

    Sometimes we just want our applications to have something different than what is given to us out of the box. This quick…

Explore content categories