UWP App Settings

In UWP applications settings have been sandboxed along with other local app content. Settings have been stored in text files, the registry and more recently the app.config file associated with the application being developed.

Normally, when an app starts it wants to load its current settings but if they don’t exist then this scenario needs to be checked and reasonable default values need to be used instead. This simple class wraps up settings access in a modular way and provides a safe mechanism for loading settings when they may not have been defined yet.

As an aside, this simple settings class also retrieves the App version and build information as a string which is often useful when displaying the version at runtime in an about box or when checking for version compatibility issues.

Source Code

The source code contains three settings properties, a string, an integer and a boolean. The SaveSettings() and LoadSettings() methods uses the C#6.0 nameof() feature to get the name of the variable being saved. This is much more resilient in a key value pair example than making up a name or copying and pasting text names around the code.

The LoadSettings() method works in reverse but calls a generic method for each of the settings variable types <T>, which ensures that valid values are safely loaded and if this is not possible, that the default value is returned.

The generic method LoadSetting<T> first checks if the property exists. If it does then it is read by its key name and returned. If it does not exist then the supplied default value is returned. If any exception is thrown then the default value is also returned.

Note that there is no return at the end of the method because all possible return paths have been handled within the body of the method.

Finally, as mentioned above the settings class wraps up getting the applications version and build information to make it easy to get this from a single point in the application code.

 The app settings source code can be download from: –

AppSettings.zip

About Open Technologies Limited

OTL specialise in the design and development of real-time software and hardware applications. We strive to innovate and bring our customers both cutting edge technologies and efficient solutions to their problems. With many years of experience working in industrial and consumer sectors we are always conscious of the need for reliable, maintainable and sustainable products. We are always looking to work with new clients and welcome the opportunity to discuss how we may be able to help you.

Contact OTL