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:
- https://github.com/microsoft/CsWinRT/issues/1809
- https://github.com/dotnet/sdk/issues/43707
- https://github.com/dotnet/sdk/issues/43746
- https://github.com/dotnet/sdk/issues/43680
- https://github.com/microsoft/CsWinRT/issues/1799
- https://github.com/microsoft/CsWinRT/pull/1816
More on AOT / Trimming support in Windows / CsWinRT:
- https://learn.microsoft.com/en-us/windows/apps/windows-app-sdk/stable-channel#native-aot-support
- https://github.com/microsoft/CsWinRT/blob/master/docs/aot-trimming.md
- Also see CsWinRT Release Notes
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)
CsWinRTRcwFactoryFallbackGeneratorForceOptOutleads to lots ofCsWinRTRcwFactoryFallbackGeneratorForceOptOutworks, but don’t combine it with<WindowsSdkPackageVersion>10.0.19041.38</WindowsSdkPackageVersion>😃This worked for me
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
got rid of the
Unsafe code may only appear if compiling with /unsafemessage, but now I’m getting the error:My .csproj file has defined TargetFramework as: