maui: Windows Build Errors

There have been some recent changes to .NET SDK for Windows introduce support for different projection profiles to be used as well as new roslyn analyzers introduced to help detect incompatibilities with your code and Native AOT. These changes can cause some issues with existing .NET MAUI projects but are generally easy to work around.

Given the complexity of how some of these pieces fit together there are a number of areas which will ship servicing fixes to address the issues:

  • MAUI .NET 9 RTM workload with an updated WindowsAppSDK reference to 1.6.x (MAUI currently references 1.5.x)
  • .NET 9 RTM SDK with fixes for overly eager analyzer execution, improved fallback profile package logic
  • WindowsAppSDK 1.6 servicing update

There are some related issues for those interested to learn more:

More on AOT / Trimming support in Windows / CsWinRT:

Warnings / Errors and Workarounds

There are a few examples of warnings/errors you may encounter below:

MVVMTKCFG0003 - This version of the MVVM Toolkit requires ‘Microsoft.Windows.SDK.NET.Ref’ version ‘10.0.19041.38’ or later. Please update to .NET SDK 8.0.109, 8.0.305 or 8.0.402 (or later). Alternatively, use a temporary ‘Microsoft.Windows.SDK.NET.Ref’ reference, which can be done by setting the ‘WindowsSdkPackageVersion’ property in your .csproj file. For your project configuration, it is recommended to set the package version to ‘10.0.19041.41’.

Could not load file or assembly ‘WinRT.Runtime, Version=2.1.0.0, Culture=neutral, PublicKeyToken=99ea127f02d97709’. The system cannot find the file specified.":"WinRT.Runtime, Version=2.1.0.0, Culture=neutral, PublicKeyToken=99ea127f02d97709

CS0433: The type ‘TextDecorations’ exists in both ‘Microsoft.WinUI, Version=3.0.0.0, Culture=neutral, PublicKeyToken=de31ebe4ad15742b’ and ‘Microsoft.Windows.SDK.NET, Version=10.0.20348.38, Culture=neutral, PublicKeyToken=31bf3856ad364e35’

To fix these errors, you can add a property to your .csproj file:

<PropertyGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'"">
    <WindowsSdkPackageVersion>10.0.19041.38</WindowsSdkPackageVersion>
</PropertyGroup>

If you see an error similar to the one below, you will need to add a partial identifier to the class mentioned in the error due to a new analyzer detecting code which is not Native AOT compatible:

CsWinRT1028: Class ‘MauiCommandBar’ implements WinRT interfaces but isn’t marked partial. Type should be marked partial for trimming and AOT compatibility if passed across the WinRT ABI.

WebView2 - Type exists in both WebView2.Core and Microsoft.WinUI

If you encounter an error similar to this:

net8.0-windows10.0.19041.0\win10-x64\intermediatexaml\WinRT.SourceGenerator\Generator.RcwReflectionFallbackGenerator\RcwFallbackInitializer.g.cs(1296,125,1296,137): error CS0433: The type ‘CoreWebView2’ exists in both ‘Microsoft.Web.WebView2.Core, Version=1.0.864.35, Culture=neutral, PublicKeyToken=2a8ab48044d2601e’ and ‘Microsoft.WinUI, Version=3.0.0.0, Culture=neutral, PublicKeyToken=de31ebe4ad15742b’

You can set this property in your project for now:

<PropertyGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'"">
  <CsWinRTRcwFactoryFallbackGeneratorForceOptOut>true</CsWinRTRcwFactoryFallbackGeneratorForceOptOut>
</PropertyGroup>

There is already an associated PR with the fix: https://github.com/microsoft/CsWinRT/pull/1816 which will be released in an upcoming servicing release.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 5
  • Comments: 19 (4 by maintainers)

Most upvoted comments

CsWinRTRcwFactoryFallbackGeneratorForceOptOut leads to lots of

Generator.WinRTAotSourceGenerator\WinRTGenericInstantiation.g.cs(2531,31,2531,47): error CS0227: Unsafe code may only appear if compiling with /unsafe

CsWinRTRcwFactoryFallbackGeneratorForceOptOut works, but don’t combine it with <WindowsSdkPackageVersion>10.0.19041.38</WindowsSdkPackageVersion> 😃

This worked for me

<PropertyGroup>
        <TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.22621.0</TargetFrameworks>
        <OutputType Condition="'$(TargetFramework)' != 'net8.0'">Exe</OutputType>

        <!-- Versions -->
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
        <TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
	<WindowsSdkPackageVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.22621.38</WindowsSdkPackageVersion>
	<CsWinRTRcwFactoryFallbackGeneratorForceOptOut Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">true</CsWinRTRcwFactoryFallbackGeneratorForceOptOut>
        <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
        <DefaultLanguage>en</DefaultLanguage>
    </PropertyGroup>

If you encounter an error similar to this:

net8.0-windows10.0.19041.0\win10-x64\intermediatexaml\WinRT.SourceGenerator\Generator.RcwReflectionFallbackGenerator\RcwFallbackInitializer.g.cs(1296,125,1296,137): error CS0433: The type ‘CoreWebView2’ exists in both ‘Microsoft.Web.WebView2.Core, Version=1.0.864.35, Culture=neutral, PublicKeyToken=2a8ab48044d2601e’ and ‘Microsoft.WinUI, Version=3.0.0.0, Culture=neutral, PublicKeyToken=de31ebe4ad15742b’

You can set this property in your project for now:

<PropertyGroup Condition=“$([MSBuild]::GetTargetPlatformIdentifier(‘$(TargetFramework)’)) == ‘windows’”"> <CsWinRTRcwFactoryFallbackGeneratorForceOptOut>true</CsWinRTRcwFactoryFallbackGeneratorForceOptOut> </PropertyGroup> There is already an associated PR with the fix: microsoft/CsWinRT#1816 which will be > released in an upcoming servicing release.

This doesn’t resolve the issue. I’m dead in the water again. Also, there’s a typo in the suggested workaround. There’s an extra " after ‘windows’.

I have a packaged WinUI 3 project that broke. Adding

<PropertyGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">
    <WindowsSdkPackageVersion>10.0.19041.38</WindowsSdkPackageVersion>
</PropertyGroup>

got rid of the Unsafe code may only appear if compiling with /unsafe message, but now I’m getting the error:

Severity	Code	Description	Project	File	Line	Suppression State
Error (active)	MSB4044	The "ResolvePackageAssets" task was not given a value for the required parameter "TargetFramework".	C:\Program Files\dotnet\sdk\8.0.403\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets	266	

My .csproj file has defined TargetFramework as:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
...