S E L L C O R P

Transforming UWP Applications for .NET 9: Modernizing Old Code -

Transforming UWP Applications for .NET 9: Modernizing Old Code

September 20, 2024 Garnet Comments Off

Microsoft has introduced new tools designed to bring Universal Windows Platform (UWP) applications to the .NET 9 ecosystem, offering developers the ability to bridge the gap between legacy code and modern .NET technologies.

The Evolution of Windows App Development

The path to today’s unified framework for .NET desktop applications has been a long one. While the Windows App SDK and Win UI provide advanced tools and designs, developers who previously built apps on Windows 10’s UWP face challenges when trying to migrate their older code. UWP, originally created to leverage new Windows APIs and the Microsoft Store’s infrastructure, tied developers to a stringent application approval process and Microsoft’s payment platform. This setup hindered flexibility, but the ecosystem has since evolved, allowing greater freedom for app distribution and supporting modern .NET features.

Key Updates in .NET 9: Ahead-of-Time (AOT) Compilation

With the upcoming release of .NET 9 and its release candidate now available, Microsoft has introduced additional tools to ease the migration of UWP applications to this new platform. A particularly notable feature is native Ahead-of-Time (AOT) compilation. This enables developers to compile their code directly into native binaries, which leads to faster application startup times. However, this does require generating separate binaries for different architectures, such as X64 and Arm64. Tools like Advanced Installer can assist by packaging multi-architecture installers, ensuring compatibility across devices.

Migrating UWP to the Windows App SDK

Transitioning UWP applications to the Windows App SDK isn’t an automatic process and often requires patience, especially when using third-party libraries that haven’t been updated. Although legacy UWP apps can still be distributed through the Microsoft Store, they miss out on new features like AOT compilation.

Microsoft recognizes the challenges associated with upgrading older codebases and has developed a toolkit to help UWP applications leverage modern .NET functionalities without requiring complete overhauls. This incremental update strategy lets developers gradually introduce new technologies like Win UI 3 while retaining parts of their existing UWP codebase, such as maintaining certain functionalities through XAML Islands. This approach has even been adopted for updating Microsoft Store applications, which are expected to feature native AOT support after the general availability of .NET 9.

Moving Away from .NET Native

One of the key advantages of migrating UWP applications to .NET 9 is the ability to bypass the older .NET Native tooling, which has limited access to modern features beyond .NET Core 2.0 and .NET Standard 2.0. By switching to the new platform, developers can incorporate up-to-date libraries, enhancing their applications with the latest .NET and Windows features.

Setting Up UWP in .NET 9

Although the process is still somewhat complex, developers can begin migrating their UWP apps using the latest Visual Studio 17.12 preview build. This includes enabling UWP support within Visual Studio, installing the Windows 11 SDK, and downloading a new XAML compiler. Developers will also need the latest nightly builds of .NET 9, as full integration will only come with later preview releases.

To get started, developers should create a new project using the .NET 9-supported UWP templates. While these templates are still in development, they allow for compatibility with existing code and are expected to fully support UWP applications once .NET 9 is released. Additional project templates will become available for scenarios like DirectX applications or building new libraries.

Overcoming Challenges in Migration

The transition to .NET 9 won’t be seamless, as developers will need to address several hurdles. For instance, certain adjustments to the project file are necessary to resolve build errors. Key properties in the project file ensure that the UWP application is compiled using the correct XAML compiler and that native AOT is enabled, which is required for publishing the app in the Microsoft Store. Developers must also ensure that all code, including any third-party libraries, is compatible with native AOT, which could require significant adjustments.

Expanding Capabilities: Integrating UWP with Win32

Aside from helping with the migration from UWP to the Windows App SDK, the new tools also enhance scenarios like XAML Islands, which allows developers to integrate UWP controls within modern Win32 applications. With .NET 9’s UWP support, developers can manage both Win32 code and UWP controls within a single project, simplifying the development process and eliminating the need for separate build tools.

Microsoft’s Commitment to Transition Tools

While there are still missing features in the current preview, such as the XAML designer, Microsoft remains committed to supporting developers through this transition. A new designer is expected to be ready by the time .NET 9 is officially launched.

It’s important to note that these tools are not intended for building brand-new UWP applications, as UWP is no longer in active development. However, they provide a clear upgrade path for legacy code, ensuring that existing applications are not abandoned as Microsoft continues to push forward with its latest technologies.

In conclusion, the migration of UWP applications to .NET 9 is both a necessity and an opportunity. With the right tools and approach, developers can future-proof their applications while taking advantage of the performance and flexibility offered by .NET’s latest features.