C# Universal Applications – Local Settings

C

At the point when pondering your universal applications information, one angle to consider is data lifetime. All in all, with regards to the lifetime of information, you have two alternatives: local data, which exists as long as the app that created it remains installed, and roaming data. Use settings to store application state and client inclinations.

The Universal Windows Platform (UWP) gives APIs to store and recover settings rapidly. At the point when an application is introduced, the framework makes a for each client application information holder on the gadget. This application information compartment is inside the application sandbox, which implies no other application will have the capacity to get to it. The framework will save the substance of the data container when the application is upgraded and will evacuate it when the client uninstalls the application.

The following source code snippet provides an easy to use class for manipulating application’s local settings:

    public class Settings
    {
        public Settings()
        {

        }

        /// <summary></summary>
        /// <typeparam name="T">DataType of the stored value</typeparam>
        /// <param name="key">The dictionary key of the stored value</param>
        /// <returns>The correspoding value</returns>
        public T GetValue<T>(string key)
        {
            return !ApplicationData.Current.LocalSettings.Values.ContainsKey(key) ?
                    default(T)
                    :
                    (T)(ApplicationData.Current.LocalSettings.Values[key]);
        }


        /// <summary></summary>
        /// <typeparam name="T">DataType of the stored value</typeparam>
        /// <param name="key">Name of the key</param>
        /// <param name="value">The value which would be stored</param>
        public void SetValue<T>(string key, T value)
        {
            if (key == default(string))

                throw new Exception("Settings Key Must Not Be NULL");

            try
            {
                ApplicationData.Current.LocalSettings.Values[key] = value;
            }
            catch(Exception)
            {
                ApplicationData.Current.LocalSettings.Values.Add(key, value);
            }
        }


        /// <summary></summary>
        /// <param name="key">The key that should be removed</param>
        public void Remove(string key)
        {
            if (key == default(string))

                throw new Exception("Settings Key Must Not Be NULL");

            try
            {
                ApplicationData.Current.LocalSettings.Values.Remove(key);
            }
            catch (Exception)
            {
                throw new Exception("Settings Key Not Found");
            }
        }
    }
Disclaimer: The present content may not be used for training artificial intelligence or machine learning algorithms. All other uses, including search, entertainment, and commercial use, are permitted.

Categories

Tags