microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.36k stars 679 forks source link

CS0246 and MSB3073 error occur when C# Project Referencing a WinUI 3 Component library #8455

Open AnnYang01 opened 1 year ago

AnnYang01 commented 1 year ago

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

  1. Create new C# WinUI Desktop Project and name MyCSDesktopProject.
  2. Create new C# Class Library (WinUI 3 in Desktop) and name MyCsClassLibrary.
  3. Add a User Control (WinUI 3) to MyCsClassLibrary.
  4. MyCSDesktopProject Referencing a MyCsClassLibrary.
  5. Create a Windows Runtime Component (C++/WinRT) project, and name it ThermometerWRC (for "thermometer Windows Runtime component").
  6. 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: x86 ThermometerWRC <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

image

NuGet package version

None

Windows version

Windows 11 (22H2): Build 22621

Additional context

This issue also reproduces on ARM64 machine.

AnnYang01 commented 1 year ago

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>
  1. Run again, all errors disappear, but still have some warnings.
  2. Reopen the solution, and run again, all warnings and errors disappear.
JesseCol commented 10 months ago

Hi, can you let us know if this still repros in WinAppSDK 1.4?

AnnYang01 commented 10 months ago

@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. image this issue was closed "Not planned", did you mean "it won't be fixed in the future"?

rcbevans commented 5 months ago

I just hit this too, is the fix to downgrade the Microsoft.WindowsAppSDK, or is it necessary to drop the target version to .NET6?