
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 NowNext up:
- The art of thinking clearly
- Remove those rogue records in Excel
- New Hyper-V cmdlets in PowerShell 4.0
- Understanding the difference between Office 2013 and Office 365
- Virtualising SQL Server using Hyper-V 3.0 and SCVMM
- The science of presenting (Part 3)
- PivotTable timelines in Excel 2013
- Recursive functions in VBA
- Troubleshooting an upgrade to Exchange Server 2013
- Troubleshooting Office 365: Real solutions
Previously
- Find a filter result without filtering in Excel
- What is new in Office 365
- Quick ways to automate in Photoshop – Part 2: Modifying an Action
- How to avoid reinventing the wheel
- How to create an e-mail template in Outlook
- How to scrape a website
- Round, RoundUp and RoundDown in Excel
- Have you ever...?
- Different communication styles, Part 1 - the best communicators know this, so should you.
- Group data in ranges of values in Excel