Dependency Injection in UWP apps

Daniel
4 min readJun 3, 2023

--

The Universal Windows Platform is a powerful framework that lets developers deploy apps from the the same codebase across different device types, such as Xbox consoles, HoloLens, and Windows 10 & 11 machines. Apps such as Disney+, WhatsApp, and Ambie all take advantage of UWP to build premium experiences on Xbox and/or PC. But while the framework supports these modern experiences, UWP apps do not have built-in dependency injection (DI) support, unlike Windows Presentation Foundation (WPF) and ASP.NET Core. Fortunately, there are easy ways to add support for dependency injection without any drawbacks.

What is Dependency Injection?

It’s a software design pattern that allows us to apply the Inversion of Control (IoC) principle. IoC increases the modularity of applications, which is vital to keep the codebase maintainable and extensible. It allows for classes to be “configurable”, allowing behaviours to change without editing the file itself because the dependencies of the file can be configured differently.

But in real terms, what does it look like? At its core, DI is simply passing a dependency as a constructor or method parameter. Here’s an example of a class that doesn’t use DI:

As you can see, the PrefixMaker class is instantiated directly by ClassWithoutDI using new PrefixMaker(). This means that ClassWithoutDI has a strongly linked dependency on PrefixMaker. If you ever want to change what prefix is used, you’d have to make edits to the ClassWithoutDI file.

Now, let’s see what it looks like when it uses dependency injection.

In the above Gist, you’ll find that we’re now passing an IPrefixMaker interface into the class. This is the dependency being injected. We’re saving that reference to _prefixMaker, and we’re using that private variable in GetGreeting(). With this setup, we can actually switch between different implementations of IPrefixMaker without making any changes to the ClassWithDI object. We can configure the class to output “Hello Vancouver” or “Bonjour Vancouver” depending on which implementation we register.

At a high-level, that’s dependency injection. Can you see the types of benefits this approach provides? Now, for the part that you actually care about: how to set it all up.

Setting up DI in UWP

To start, you’ll need the Microsoft.Extensions.DependencyInjection nuget package installed on your Universal Windows app project. This is an official DI package from Microsoft that contains code for dependency registration.

Next, create a partial class called App.Configuration.xaml.cs in your project. It should look like this

Inside the configuration file, add code as follows:

Now, in App.xaml.cs add this if block in the OnLaunched method:

The ConfigureServices method in App.Configuration.xaml.cs is where you’ll be registering your dependencies. There are different lifetimes to choose when you register, and you can read more about that here: https://stackoverflow.com/a/38139500. In short, AddTransient means that each time a class requests for that particular dependency, a new instance is always returned. AddSingleton means that each time a class requests for that particular dependency, a single shared instance is returned.

Using DI in UWP pages and controls

Now that you’re all set up, how do you actually use it?

The above Gist shows a MainPage.xaml.cs that retrieves the DI-registered class and uses it to display text on the page. The approach of using App.Services.GetRequiredService<T>() is the primary way of using the classes that you’ve registered in a UWP app’s Page, UserControl, or other UI-facing objects.

UWP 🤝🏽 DI

While UWP doesn’t have built-in support for dependency injection, we just saw above how to enable DI in your UWP app quickly and efficiently. Want to play with actual running code? Here’s a sample repo used by this article: https://github.com/dpaulino/UwpDependencyInjectionSample.

And if you’re interested in seeing DI used in a real app (not just a sample), check out Ambie White Noise. It’s an open source app that plays white noise and nature sounds to help people relax. It’s used by over 30k users, and its rapid growth in features was primarly thanks to this DI system. Ambie can be found here: https://github.com/jenius-apps/ambie.

Hope you can implement dependency injection in your UWP apps today!

--

--

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.