UWP apps with appsettings

Daniel
4 min readJan 24, 2021

--

When building apps that are tied to a backend service, you often have different URLs, secrets, or keys depending on your environment. In ASP.NET core projects, the appsettings.json file can be used to alternate between these environment variables, but what about for UWP apps?

Microsoft MVP Martin Zikmund has a great writeup on how to use ASP.NET’s existing infrastructure for appsettings.json within UWP apps. Make sure to take a look at this approach.

There also are other ways to achieve an appsettings-like solution. One approach I found was to use UWP’s existing .resw file infrastructure.

AppSettings class in a UWP app like Ambie White Noise app

Appsettings.resw

As I was building my open source, white noise app called Ambie, I wanted a simple way to have debug environment variables that could be easily updated in my build pipeline to use production variables. I saw Martin’s solution, but I was curious if I could find another way that leveraged existing UWP features. The .resw files available to UWP apps turned out to be simple to implement, and even simpler to use within the app thanks to UWP’s ResourceLoader class. Follow the steps below to add one to your own project.

Create the resw file

Right click on your Universal Windows project and add an appsettings.resw to the root of the project.

Create IAppSettings interface

My app is setup with dependency injection, so I decided to create an interface representing the appsettings.resw strings. This was added to Ambie’s .NET Standard project. Here’s Ambie’s interface.

Create AppSettings class

In your Universal Windows project, create the class that implements IAppSettings. As mentioned earlier, ResourceLoader makes the usage of resw files easy. In this class’s constructor, simply use the ResourceLoader to read the strings from your resw file. Make sure that the input for GetForCurrentView() is the name of the resw file. Here’s Ambie’s class.

Usage

You can now easily use these app settings anywhere in your app. During debugging, edit your resw file to have debug variables. Then use the interface or the class in your app like the below example. Source code is here.

Production variables in Azure Pipeline

Ambie is set up to use Azure Pipelines for its release builds. Feel free to look at Ambie’s YAML file to see what a pipeline looks like for UWP apps. Creating this pipeline will be left as an exercise for the reader. Below are the steps to add production variables to your build pipeline.

Add new variable group in Azure Pipelines Library

See these instructions on how to add a new variable group and how to use it in your pipeline. Below I’ll show how I do it for Ambie, but these steps may be different depending on the needs of your project.

Navigate to Azure Pipeline’s library page, and add a new variable group. Then add variables.

Enable variable group in your build pipeline

Make sure that your YAML pipeline is configured to use your variable group. You can find this inside of Ambie’s YAML file.

Edit the resw with PowerShell

In order to use production values, we’ll need to modify the resw file in the build pipeline. Notice in Ambie’s YAML file, there is a PowreShell task that edits the resw file. Copy this into your build pipeline before the compilation task. And now, you’re done!

In conclusion

UWP’s ResourceLoader makes for a simple way to add some debug/production environment variables to your app. While it does not operate with the same features as ASP.NET core’s appsettings.json system, it is a good alternative for simple projects such as Ambie.

--

--

Daniel
Daniel

Written by Daniel

I’m a software engineer at Microsoft, and I build Windows apps. I created Nightingale REST client. My stories are personal & not Microsoft’s.

No responses yet