Article

UWP (.NET Native) Migration to UWP (.NET 9)

UWP (.NET Native) startup performance after AOT is still better than UWP (.NET 9) startup performance after AOT. Taking OneBrowser as an example, when launching OneBrowser, the UWP (.NET 9) version takes approximately 2x longer to start.

Development 3 min

UWP (.NET Native) to UWP (.NET 9) Experience

UWP (.NET Native)

Using the .NET Native runtime, it does not support the latest .NET 9 features (such as the latest C# features or performance optimizations). Compile using the .NET Native tool chain.

UWP (.NET 9)

UWP apps based on .NET 9 builds on all supported versions of Windows 10 and Windows 11. They use XAML and C# and support Native AOT compilation for improved performance and reduced dependency on .NET Native.

Conclusion

Comparison UWP (.NET Native) UWP (.NET 9)
Performance Fast Slow
Project Files Complex Simple and Unified
OneBrowser Splash screen flashes by Splash screen has a noticeable wait time
Binding Direct Binding Requires declaring the [Bindable] attribute on almost all VM classes
Binding Type Direct Binding Only supports WinRT.ObjectReferenceWithContext<WinRT.Interop.IUnknownVftbl> in non-[Bindable] declarations, such as string
DataContext Source Arbitrary Definition Must declare a partial class, otherwise OnPropertyChanged may not trigger a secondary XAML fetch
.Winmd Direct Reference Can only reference source code projects. If there is no source code, use CSWinRT to encapsulate and project it before referencing

Development Suggestions

Using UWP Development (.NET Native); Using .NET 9 Directly Uses WINUI3

Whether developing with UWP (.NET Native) or UWP (.NET 9), if you have a UWP (.NET Native) project, please use Windows 10. Developing with both frameworks on Windows 11 may cause issues, resulting in the following error:


Exited, but the CoreCLR Start event was not raised. Please ensure that the target process is configured to use .NET Core. This is not unexpected if the target process is not running .NET Core.

The cause is unknown. This issue occurs in Debug mode after upgrading to Windows 11 23H2. Release mode may function normally due to AOT.

1. [Bindable] Example

[Important] For .NET 9-UWP projects, due to AOT reflection, the binding type must be marked with the [Bindable] attribute, which can resolve most binding issues.


[Bindable]
public class DeskViewItemVM : ViewModelAbstract
{
/// <summary>
/// Original image
/// </summary>
public BitmapImage EntireImage
{
get;set;
}
}

2. OnPropertyChanged Secondary Triggering OneWay Example

[Important] When binding in XAML, MainPageVM must be declared as a partial class. Otherwise, setting the ImageUrl will not trigger the pull event again.


base.OnPropertyChanged(nameof(base.ResourceLocalPath));

[Bindable]
public partial class MainPageVM : ResourceCacheVMAbstract
{
public string ImageUrl
{
get { return this.ResourceUrl; }
set
{
this.ClearResourcePathCache();
this.ResourceUrl = value;
base.OnPropertyChanged(nameof(base.ResourceLocalPath));
}
}

}

3. OneBrowser - WebView Interaction

UWP Windows Runtime Component (.NET 9 UWP)

This replaces the original UWP Windows Runtime Component (.NET Native). The UWP Windows Runtime Component (.NET 9 UWP) project references the following package to generate the RT projection:


<PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.2.0" />

Then, implement JS interaction with OneBrowser’s webView using the original method.

4. OneBrowser - WebView2 Interaction

Original project structure:

AddHostObjectBridgeComponent: UWP Runtime (.NET Native) native)

WinRTAdapter: UWP Runtime (C++/WinRT)

OneBrowser: UWP (.NET native)

After Migration

AddHostObjectBridgeComponent: UWP Runtime (.NET native)

WinRTAdapter: UWP Runtime (C++/WinRT) [Migrate the Bridge class from AddHostObjectBridgeComponent]

OneBrowser: UWP (.NET 9)