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

Chapter 1 A Tour of .NET 6

We’ll get deeper into the command line in Chapter 3 of this book.

Desktop Development

The desktop to this day remains a very important target for applications. Not every type of application is suited to be a web application. That’s why Microsoft brought WPF and WinForms back into active development with the release of .NET Core 3. Not only that, but they are also expanding the possibilities for desktop developers.

Before they brought WPF and WinForms back, however, they were considered legacy. It was time for a new modern desktop application framework. The Universal Windows Platform saw the light of day years ago as the cross-device Windows app framework. It promised a “build once, run everywhere” experience, and they mostly delivered. But since UWP was mostly targeted at mobile experiences through Windows on tablets and phones, it kept lacking in features to make it a true successor to

WPF. Today UWP has matured into a decent application framework that can deliver modern, fast applications, but it still does not have all features WPF has.

To close this gap, Microsoft has introduced the Windows App SDK. The Windows App SDK aims to bring all desktop development options closer together. The Windows App SDK brings a set of Windows APIs that are decoupled from Windows itself. The APIs are released via NuGet packages so that they can be updated out of band with the operating system.

The Windows App SDK consists of a few parts. One of the major parts is WinUI 3. WinUI 3 decouples the XAML-based UI controls of UWP from the framework and makes them available as NuGet packages. This makes it possible for Microsoft to update their control library apart from the operating system. It also makes it possible for application developers to support newer UI features on older versions of Windows, without the need of operating system upgrades.

Getting started with The Windows App SDK can be done in a few different ways, which we’ll get deeper into in Chapter 4. The fastest way to get into Project Reunion is by using the WinUI templates in Visual Studio.

12

Chapter 1 A Tour of .NET 6

Figure 1-8.  WinUI project templates

The Blank App, Packaged template creates a new UWP project, including a package manifest for distribution through the Microsoft Store. Figure 1-9 shows the newly created solution.

Figure 1-9.  A fresh WinUI project in Visual Studio

If you’ve ever built a UWP app, you might not see any difference at first sight. However, under the hood all the XAML controls this app is using are coming from a NuGet package, not from the UWP SDK like it used to. Have a look at the dependencies shown in Figure 1-13.

13

Chapter 1 A Tour of .NET 6

Figure 1-10.  Dependencies for a WinUI project

Figure 1-10 shows that the Windows SDK is still referenced; we still need that for all the system calls, like creating the app’s window and drawing everything on screen. The controls however all come from the Microsoft.WindowsAppSDK NuGet package.

An easy way to discover all the controls available in WinUI is by browsing the control gallery app available on the Windows Store www.microsoft.com/store/ productid/9P3JFPWWDZRC.

The source code of the gallery app is available at https://github.com/Microsoft/ Xaml-Controls-Gallery/.

Figure 1-11.  The WinUI 3 Control Gallery Sample app

14

Chapter 1 A Tour of .NET 6

The Control Gallery Sample App is not only a demo application, but it provides XAML snippets and links to official documentation for every control in the toolkit.

Figure 1-12.  XAML snippets and links to documentation in the Control Gallery app

Blazor

Blazor is Microsoft’s answer to front-end web frameworks like Angular, Vue, and React. Its main selling point is that you can write these web apps in C# rather than JavaScript. You can still inject JavaScript and consume JavaScript libraries where you need to, but everything can basically be done through C#.

Blazor currently comes in two flavors:

•\

Blazor Server

•\

Blazor WebAssembly

Blazor Server runs all its C# logic in an ASP.NET context on a webserver and sends back the results. This is done through a SignalR connection instead of page reloads like ASP.NET MVC. This version of Blazor was released in .NET Core 3. Blazor WebAssembly was released in the .NET 5 timeframe. It runs all of its C# logic in the

browser, not through some form of C# to JavaScript transpiling but through the magic of WebAssembly.

15

Chapter 1 A Tour of .NET 6

WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable compilation target for programming languages, enabling deployment on the web for client and server applications.

Source: https://webassembly.org

Like the official description says, Wasm is a virtual execution environment in your browser. Wasm is an official W3C spec https://www.w3.org/wasm/.

Since Wasm is an environment that runs native code, Microsoft was able to get a version of .NET running inside of Wasm. With this, Blazor Wasm was born.

.NET 6 brings Blazor to the desktop. With Blazor Desktop, we can build hybrid applications, parts can be native UI, and parts can be web UI. It’s not unlike Electron, but it differs in that applications can actually be, for example, part WPF and part web. Figure 1-13 shows a Blazor Desktop application that mixes native WPF controls with a Blazor Web Component.

Figure 1-13.  Mixing WPF UI with Blazor Web Components (Source: Microsoft)

As you can see in Figure 1-13, Blazor Desktop goes beyond just mixing two UI stacks. The data is shared between them. The counter increment button is web UI, while the button to see the counter value and the message box showing the value are both

16