Managing application settings in Windows Store Apps

 May 19, 2015

Windows 8.1 applications can save and retrieve settings using local storage in a very straightforward way. In today's blog post, I will guide you through the steps to get this done.

The following line of code is giving us a reference to an ApplicationDataContainer which we will use to access local settings storage.

Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;

Now, the variable localSettings is all you need to read and write simple application settings, for example, the ID of the last customer processed, the printer page orientation preferred by the user or anything else that could be useful to give a better experience to the app users.

localSettings.Values["ID"] = 101; localSettings.Values["orientation"] = "Portrait"; localSettings.Values["defaultFontSize"] = 14; localSettings.Values["lastAccess"] = DateTime.Now;

Note that you can assign different data types to values because it will be always stored as object. When you read, you should cast it to the appropriate data type.

if(localSettings.Values.ContainsKey("ID")) { int custID = (int) localSettings.Values["ID"]; }

Also, note that it is very important to always check if the setting was ever saved before you use it.

This API also provides a mechanism to store composite settings, which are sets of settings managed as a single unit, as we demonstrate next.

Windows.Storage.ApplicationDataCompositeValue composite = new Windows.Storage.ApplicationDataCompositeValue(); composite["item1"] = 123; composite["item2"] = "value 2"; localSettings.Values["Composite"] = composite;

To read the composite, we proceed with the following:

if(localSettings.Values.ContainsKey("Composite") { Windows.Storage.ApplicationDataCompositeValue composite = (Windows.Storage.ApplicationDataCompositeValue) localSettings.Values["Composite"]; if(composite["item1"] != null){ int i = (int) composite["item1"]; } }

Essentially, we are grouping a set of settings together and giving a name to the group. The API goes one more step further by supporting containers. Inside a container, we can have simple settings or composite settings. The next snippet demonstrates containers.

Windows.Storage.ApplicationDataContainer container = localSettings.CreateContainer("Container1", Windows.Storage.ApplicationDataCreateDisposition.Always);

The value of Always for the second parameter means that the container should be created if it does not already exist. Once you have a container you use values in the same way as with localSettings.

container.Values["value1"] = 123;

To delete settings, you need to use the Remove method:

localSettings.Values.Remove("item1");

To delete containers, you need to use the DeleteContainer method:

localSettings.DeleteContainer("Container1");

To learn more about Windows Store Apps development, I recommend you take a look at New Horizons' 20484: Essentials of Developing Windows Store Apps Using C# training course.

How do your Excel skills stack up?   

Test Now  

About the Author:

Newton Godoy  

With over 17 years of in-class training experience and over 16 years of industry experience, Newton offers students a wealth of real-world technical knowledge and expertise in the areas of .NET application development, SQL Server and SharePoint Server. After spending several years lecturing as a professor, Newton found his true calling and began his career as a MCT. He worked as a technical trainer for some of Brazil's and Australia’s largest corporate training organisations before finally finding a home with New Horizons where he is now one of our top trainers. Newton brings a thorough mentoring capability to the classroom where he can advise on technical issues and challenges often beyond the scope of the course curriculum. His combination of technical knowledge and instructor experience make him one of the most respected instructors within the IT training industry.

Read full bio
top
Back to top