CS0246 and MSB3073 error occur when C# Project Referencing a WinUI 3 Component library
Describe the bug
I create new C# Desktop Project and this project Reference a C# Library User Control, finally, I create a functional Windows Runtime component (WinUI 3 Component Library) following the below steps, after build this solution, CS0246 and MSB3073 error will occur.
Steps to reproduce the bug
- Create new C# WinUI Desktop Project and name MyCSDesktopProject.
- Create new C# Class Library (WinUI 3 in Desktop) and name MyCsClassLibrary.
- Add a User Control (WinUI 3) to MyCsClassLibrary.
- MyCSDesktopProject Referencing a MyCsClassLibrary.
- Create a Windows Runtime Component (C++/WinRT) project, and name it ThermometerWRC (for "thermometer Windows Runtime component").
- Create a functional Windows Runtime component (WinUI 3 Component Library) <1>Open the MyCSDesktopProject solution file in Visual Studio. This should contain the UserControl and the Class Library created earlier <2>Right click on the solution node and select Add-> Existing Project... <3>Select Thermometer WRC.vcxproj <4>Right click on the MyCsClassLibrary project node and select Manage Nuget Packages <5>In the Browse Tab of the Nuget Package Manager enter cswinrt in the search bar. Install the latest stable version <6>Right click on the MyCsClassLibrary project node and select Add->Project Reference <7>Click the checkbox for ThermometerWRC and click OK. Your MyCsClassLibrary.csproj should have a reference to the ThermometerWRC.vcxproj file. <8>Add the following properties to the first PropertyGroup in the MyCsClassLibrary.csproj file: <PlatformTarget>x86</PlatformTarget> <CsWinRTIncludes>ThermometerWRC</CsWinRTIncludes> <9> Right click on the MyCsDesktopProject Node and select Manage Nuget Packages <10>In the Browse Tab of the Nuget Package Manager enter cswinrt in the search bar. Install the latest stable version. <11>In UserControl1.xaml.cs include the following library at the top of the file: using ThermometerWRC; <12>Include the following in the Button_Click method of the UserControl1.xaml.cs file. Thermometer t = new Thermometer(); t.AdjustTemperature(10); <13> Build and run the solution (F5).
Actual result: 5 errors and 5 warnings are in Error List. Severity Code Description Project File Line Suppression State Error CS0246 The type or namespace name 'ThermometerWRC' could not be found (are you missing a using directive or an assembly reference?) MyCsClassLibrary C:\Users\v-annyang.FAREAST\source\repos\MyCSDesktopProject\MyCsClassLibrary\UserControl1.xaml.cs 15 Active Warning MSB3851 This project targets "Windows, Version=10.0.19041.0", but it is attempting to reference "C:\Users\v-annyang.FAREAST\source\repos\MyCSDesktopProject\Debug\ThermometerWRC\ThermometerWRC.winmd" targeting "UAP,Version=10.0.22621.0", which is invalid. MyCSDesktopProject C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets 2677 Error CS0246 The type or namespace name 'Thermometer' could not be found (are you missing a using directive or an assembly reference?) MyCsClassLibrary C:\Users\v-annyang.FAREAST\source\repos\MyCSDesktopProject\MyCsClassLibrary\UserControl1.xaml.cs 35 Active Error CS0246 The type or namespace name 'Thermometer' could not be found (are you missing a using directive or an assembly reference?) MyCsClassLibrary C:\Users\v-annyang.FAREAST\source\repos\MyCSDesktopProject\MyCsClassLibrary\UserControl1.xaml.cs 35 Active Warning Could not read the Windows SDK's Platform.xml at C:\Program Files (x86)\Windows Kits\10\Platforms\UAP\10.0.19041.0\Platform.xml MyCSDesktopProject C:\Users\v-annyang.FAREAST\source\repos\MyCSDesktopProject\MyCSDesktopProject\EXEC 1 Warning MSB3073 The command ""C:\Users\v-annyang.FAREAST.nuget\packages\microsoft.windows.cswinrt\2.0.2\cswinrt.exe" @"C:\Users\v-annyang.FAREAST\source\repos\MyCSDesktopProject\MyCSDesktopProject\obj\x86\Debug\net6.0-windows10.0.19041.0\win10-x86\Generated Files\cswinrt.rsp"" exited with code 1. MyCSDesktopProject C:\Users\v-annyang.FAREAST.nuget\packages\microsoft.windows.cswinrt\2.0.2\build\Microsoft.Windows.CsWinRT.targets 191 Warning MSB3851 This project targets "Windows, Version=10.0.19041.0", but it is attempting to reference "C:\Users\v-annyang.FAREAST\source\repos\MyCSDesktopProject\Debug\ThermometerWRC\ThermometerWRC.winmd" targeting "UAP,Version=10.0.22621.0", which is invalid. MyCsClassLibrary C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets 2677 Error Could not read the Windows SDK's Platform.xml at C:\Program Files (x86)\Windows Kits\10\Platforms\UAP\10.0.19041.0\Platform.xml MyCsClassLibrary C:\Users\v-annyang.FAREAST\source\repos\MyCSDesktopProject\MyCsClassLibrary\EXEC 1 Error MSB3073 The command ""C:\Users\v-annyang.FAREAST.nuget\packages\microsoft.windows.cswinrt\2.0.2\cswinrt.exe" @"C:\Users\v-annyang.FAREAST\source\repos\MyCSDesktopProject\MyCsClassLibrary\obj\Debug\net6.0-windows10.0.19041.0\Generated Files\cswinrt.rsp"" exited with code 1. MyCsClassLibrary C:\Users\v-annyang.FAREAST.nuget\packages\microsoft.windows.cswinrt\2.0.2\build\Microsoft.Windows.CsWinRT.targets 191 Warning MSB3851 This project targets "Windows, Version=10.0.19041.0", but it is attempting to reference "C:\Users\v-annyang.FAREAST\source\repos\MyCSDesktopProject\Debug\ThermometerWRC\ThermometerWRC.winmd" targeting "UAP,Version=10.0.22621.0", which is invalid. MyCSDesktopProject C:\Program Files\Microsoft Visual Studio\2022\Preview\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets 2677
Expected behavior
No error occur.
Screenshots

NuGet package version
None
Windows version
Windows 11 (22H2): Build 22621
Additional context
This issue also reproduces on ARM64 machine.
If change the MyCsClassLibrary project and MyCSDesktopProject's Target Platform Version to below on VS 17.6.0 Preview 1.0 [33414.285.d17.6].
<TargetFramework>net6.0-windows10.0.22621.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17134.0</TargetPlatformMinVersion>
- Run again, all errors disappear, but still have some warnings.
- Reopen the solution, and run again, all warnings and errors disappear.
Hi, can you let us know if this still repros in WinAppSDK 1.4?
@JesseCol this issue still reproes in WinAppSDK 1.4! please see the screenshot.
Due to the repro project is too big to upload, you can refer to https://devdiv.visualstudio.com/DevDiv/_wiki/wikis/DevDiv.wiki/24416/WinUI-BVTs?anchor=c%23-project-referencing-a-winui-3-component-library to create a repro project.
this issue was closed "Not planned", did you mean "it won't be fixed in the future"?
I just hit this too, is the fix to downgrade the Microsoft.WindowsAppSDK, or is it necessary to drop the target version to .NET6?
I wonder if this bug could also be responsible for problems in MAUI - https://github.com/dotnet/maui/issues/32729
I understand that on WIndows MAUI uses WinUI 3 and the symptoms are the same I think - incorrect errors saying that the referenced project targets a newer version of Windows than it actually does, and no apparent workaround.
Given the MAUI team closed my issue as a duplicate of this one (which may or may not be the case), I'll add this workaround here in case it assists someone. The problem is so annoying at there's isn't really an issue, just that VS thinks that there is, and this seems sufficient to completely break the build process.
After a lot of wasted time and experimentation I found that (in my case at least) the following workaround allows me to disable the check and thus perform the build (proving even further that it's the check that's the issue and not any actual real problem).
<Project> <!-- Note Sdk attribute is removed from Project element -->
<!-- New line below -->
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
<PropertyGroup>
<TargetFramework>net10.0-windows10.0.19041.0</TargetFramework>
<!-- Might need other properties here too, per your project -->
</PropertyGroup>
<!-- Another new line below (and order seems to matter, but no clue why) -->
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
<!-- Next line is what actually disables the broken check -->
<Target Name="FindInvalidProjectReferences" DependsOnTargets="$(FindInvalidProjectReferencesDependsOn)" />
<!-- Rest of existing .csproj file as normal -->