Book Image

Learn WinUI 3.0

By : Alvin Ashcraft
5 (1)
Book Image

Learn WinUI 3.0

5 (1)
By: Alvin Ashcraft

Overview of this book

WinUI 3.0 takes a whole new approach to delivering Windows UI components and controls, and is able to deliver the same features on more than one version of Windows 10. Learn WinUI 3.0 is a comprehensive introduction to WinUI and Windows apps for anyone who is new to WinUI, Universal Windows Platform (UWP), and XAML applications. The book begins by helping you get to grips with the latest features in WinUI and shows you how XAML is used in UI development. You'll then set up a new Visual Studio environment and learn how to create a new UWP project. Next, you'll find out how to incorporate the Model-View-ViewModel (MVVM) pattern in a WinUI project and develop unit tests for ViewModel commands. Moving on, you'll cover the Windows Template Studio (WTS) new project wizard and WinUI libraries in a step-by-step way. As you advance, you'll discover how to leverage the Fluent Design system to create beautiful WinUI applications. You'll also explore the contents and capabilities of the Windows Community Toolkit and learn to create a new UWP user control. Toward the end, the book will teach you how to build, debug, unit test, deploy, and monitor apps in production. By the end of this book, you'll have learned how to build WinUI applications from scratch and modernize existing WPF and WinForms applications using WinUI controls.
Table of Contents (20 chapters)
1
Section 1: Introduction to WinUI and Windows Applications
8
Section 2: Extending WinUI and Modernizing Applications
13
Section 3: Build and Deploy on Windows and Beyond

Using DI with ViewModel classes

All of the popular MVVM frameworks include a DI container to manage dependencies. Because we are handing MVVM ourselves, we will use a DI container that isn't bundled with any MVVM framework. Microsoft has included its own DI container in ASP.NET Core that is lightweight and easy to use. Luckily, this container is also available to other types of .NET projects, via a NuGet package.

In the MyMediaCollection project, open NuGet Package Manager and search for Microsoft.Extensions.DependencyInjection:

Figure 4.1 – Microsoft's DI NuGet package

Select the package and install the latest available version. After the installation completes, close the NuGet Package Manager tab and open App.xaml.cs. We have to make a few changes here to start using the DI container.

Microsoft implements a DI container through a class called ServiceCollection. As the name implies, it is intended to create a collection of services for...