Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
vermeir_nico_introducing_net_6_getting_started_with_blazor_m.pdf
Скачиваний:
19
Добавлен:
26.06.2023
Размер:
11.64 Mб
Скачать

Chapter 4 Desktop Development

public List<Product> Products

{

get => _products; set

{

if (value == _products)

{

return;

}

_products = value;

PropertyChanged?.Invoke(this, new PropertyChangedEventArgs (nameof(Products)));

}

}

...

The INotifyPropertyChanged interface contains one member, the PropertyChanged event. We fire this in the set method of the Products property. By firing this event, we notify the datagrid that its data has changed and that it needs to refresh its UI. We dive deeper into XAML, binding, and the MVVM design pattern in Chapter 6 of this book. Chapter 6 handles MAUI, Microsoft’s cross-platform framework.

Windows App SDK

As we have seen, there are different ways of building desktop applications for Windows. Each platform, being it WinForms or WPF, comes with its own specific way of interacting with the operating system. Back when .NET 6 was in its planning phase, a new project was announced by Microsoft under the name Project Reunion. Project Reunion promised to unify the way desktop applications were build and were interacting with the operating system. Eventually Project Reunion got renamed to the Windows App SDK.

The Windows App SDK is a set of components, tools, and a unified API that can be used to build applications for Windows 10 and Windows 11. The Windows App SDK does not replace any frameworks; it’s more of an add-on, something that complements current frameworks to provide a more consistent way of working.

The Windows App SDK consists of several features.

109

Chapter 4 Desktop Development

Table 4-1.  Windows App SDK features

Feature

Description

 

 

WinUI 3

The UI components for building Windows applications

DWriteCore

Cleartype text rendering using a device-independent text layout

 

system

MRT core

Manage translations and images in multiple languages and scales

App instancing

Control over applications being able to run multiple instances of

 

themselves

Rich activation

Launch your app through a URI or from another app

Power management

Inspect how your app affects the device’s power usage

App windows management

Create and manage app windows

Push notifications

Send rich push notifications from Azure to your app

Deployment

Deploy the Windows App SDK runtime with your app

 

 

The easiest way to get started with the Windows App SDK is by downloading and installing the Visual Studio 2022 extension from https://aka.ms/windowsappsdk/ stable-vsix-2022-cs. This extension will install the WinUI project templates in Visual Studio.

Besides the extension, we also need the Universal Windows Platform, .NET Desktop, and Desktop Development with C++ workloads installed from the Visual Studio Installer.

Building a Windows App SDK application

From Visual Studio, we create a new solution. In the project type selection window, we can filter on WinUI. The WinUI 3 templates contain the references to the Windows App SDK. The SDK is fully wired up and ready to go.

110

Chapter 4 Desktop Development

Figure 4-30.  WinUI templates

There are two types of WinUI applications, packaged applications and unpackaged applications. Unpackaged applications result in an executable that we ourselves are responsible for to put into an installer; these types of applications are used when installing through an MSI or setup.exe wizard. Packaged applications on the other hand are fully prepared to be installed using MSIX. MSIX is Microsoft’s packaging format; it is an easy way for users to install, update, and uninstall applications on their system. The biggest advantage to MSIX is that every application installed through MSIX works with a sandboxed registry and a sandboxed filesystem, meaning if we uninstall the application, all traces of it are effectively removed from the system. This prevents the slowing down of computers because of registry keys that keep floating around.

There are two app templates, one packaged with MSIX and one packaged with MSIX using a Windows Application Packaging (WAP) project. The second type will generate

a solution with two projects. The second project is only there to configure and generate the MSIX file using the package manifest. This is mostly there for backward compatibility reasons since the WAP projects have been around ever since MSIX was introduced

with UWP.

111

Chapter 4 Desktop Development

As you might have seen, there is no template for creating unpackaged apps. An unpackaged app can be created by selecting the packaged app without WAP template and adding the WindowsPackageType setting with value None to the project file. For this example, we will use the Blank App, Packaged project type, without a WAP project. Select the template and create a new solution. Figure 4-31 shows the generated project.

Figure 4-31.  Packaged Windows App SDK project

The dependencies clearly show the reference to the Windows App SDK. The Package.appxmanifest is used for setting app preferences, like preferred orientation, the icons to be shown in the Microsoft Store should we want to publish our application there, and package versioning. The app.manifest file is an XML file that contains some startup settings. Listing 4-23 shows the default content.

Listing 4-23.  app.manifest file

<?xml version="1.0" encoding="utf-8"?>

<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"> <assemblyIdentity version="1.0.0.0" name="ApressWinUiDemo.app"/>

<application xmlns="urn:schemas-microsoft-com:asm.v3"> <windowsSettings>

<!-- The combination of below two tags have the following effect:

1)Per-Monitor for >= Windows 10 Anniversary Update

2)System < Windows 10 Anniversary Update

112