App Settings for Xamarin.Forms – Part 5

 

All code can be found in my Github repository.

If you’re a Web developer (or used to be one like me 😊), you’re probably familiar with  the concept of saving application settings in web.config.
When you want to save for example a connection string, it’s difficult to hard code it as we need to change it when you it’s on production.

As mentioned from my previous posts, i’m building an app from A-Z hence in order to save any keys, we have to save it somewhere in the app . I’ll be saving two settings at the moment: Shopify Storefront URL and Shopify access token.

Unfortunately, Xamarin.Forms doesn’t support this for free, but you know what? we don’t care we can do it by ourselves.

Note: There are many other ways to this but personally i prefer this way as you’ll be aware of all changes plus, i’m not a big fan of Nugget packages.

Settings file (config.xml)

Follow these steps to make a config.xml file and read the settings out of it.

  1. In the PCL project that we’re working on, right click and add new file, and choose Empty XML file and name it config as show in this screenshot:
    Screen Shot 2017-12-03 at 10.51.54 PM
  2. Make sure to mark this file as embedded resource by right clicking on generated file and choose EmbeddedResource from Build Actions.
    Screen Shot 2017-12-03 at 10.55.54 PM
  3. Now in this file let’s add our setting as shown below:

    <?xml version=1.0 encoding=UTF-8 ?>
    <config>
      <shopify-storefront-url>YourStoreFrontUrlHere</shopify-storefront-url>
      <shopify-access-token>YourAccessToeknHere</shopify-access-token>
    </config>

  4. Wicked, now in order to retrieve these settings from code, ill create a singleton class.
    In the Helper folder we will create Settings.cs file and write this code below:


    namespace PuzzlersJordan.Helpers
    {
        public sealed class Settings
        {
            private Settings() { }

            private static readonly Settings instance = new Settings();

            public static Settings Instance
            {
                get => instance;
            }

            public string ShopifyAccessToken
            {
                get => GetSettingValueFor(Constants.ShopifyAccessTokenConfigKey);
            }

            public string ShopifyStoreFrontUrl
            {
                get => GetSettingValueFor(Constants.ShopifyStoreFrontUrlConfigKey);
            }

            private string GetSettingValueFor(string key)
            {
                var type = this.GetType();
                var resource = PuzzlersJordan.config.xml;
                using (var stream = type.GetTypeInfo().Assembly.GetManifestResourceStream(resource))
                using (var reader = new StreamReader(stream))
                {
                    var doc = XDocument.Parse(reader.ReadToEnd());
                    return doc.Element(config).Element(key).Value;
                }
            }
        }
    }

    As shown, this singleton class will retrieve any added settings by adding only properties and in the getter of each of these properties we pass the key of that setting. The main function here is GetSettingValueFor which will read the xml file and retrieve the required  key.
    These keys can be found in the constants class where we save constants in.

        public static class Constants
        {
            public static string ShopifyAccessTokenConfigKey = shopify-access-token;
            public static string ShopifyStoreFrontUrlConfigKey = shopify-storefront-url;
        }

You’re now set to consume Settings class and get any added settings.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s