在VScode中构建WPF应用程序给了我错误MSB4062



我正在尝试在Visual Studio Code中的WPF(.Net Core 3.1(上执行构建(这次不是Visual Studio,因为我需要一个更轻量级的编辑器(。通常我的所有项目都可以很好地构建,但是这个WPF项目给了我以下错误:

C:Program Files (x86)dotnetsdk3.1.100SdksMicrosoft.NET.Sdk.WindowsDesktoptargetsMicrosoft.
WinFX.targets(225,9): error MSB4062: The "Microsoft.Build.Tasks.Windows.MarkupCompilePass1" task could not be loaded from
the assembly C:Program Files %28x86%29dotnetsdk3.1.100SdksMicrosoft.NET.Sdk.WindowsDesktoptoolsnetcoreapp2.1PresentationBuildTasks.dll. Could not load file or assembly 'C:Program Files 
%28x86%29dotnetsdk3.1.100SdksMicrosoft.NET.Sdk.WindowsDesktoptoolsnetcoreapp2.1PresentationBuildTasks.dll'. The system cannot find the path specified.
Confirm that the <UsingTask> declaration is correct, that the assembly and all 
its dependencies are available, and that the task contains a public class that 
implements Microsoft.Build.Framework.ITask. [E:ui_wpf_testui_wpf_test.csproj]
The build failed. Fix the build errors and run again.

我听说它与MSbuild有关,但我不知道如何正确解决此问题

我在GitHub上找到了一个解决方案。您必须替换以下%ProgramFiles(x86)%dotnetsdk3.1.100SdksMicrosoft.NET.Sdk.WindowsDesktoptargetsMicrosoft.WinFx.props行:

<_PresentationBuildTasksAssembly Condition="'$(_PresentationBuildTasksAssembly)'==''">$([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)..tools$(_PresentationBuildTasksTfm)PresentationBuildTasks.dll'))</_PresentationBuildTasksAssembly>

将其替换为:

<_PresentationBuildTasksAssembly Condition="'$(_PresentationBuildTasksAssembly)'==''">$(MSBuildThisFileDirectory)..tools$(_PresentationBuildTasksTfm)PresentationBuildTasks.dll</_PresentationBuildTasksAssembly>

这最终对我有用。

小错误。 应该在即将推出的新 3.1.103 中修复,而不是现在的 3.1.102。

问题是路径Program Files (x86)' where(x86(is escaped to%28x86%29',导致找不到路径。

通过转义路径进行临时修复:

转到:

C:\Program Files (x86(\dotnet\sdk\3.1.102\Sdks\Microsoft.NET.Sdk.WindowsDesktop\targets

编辑Microsoft.WinFx.props并使用具有路径的Unescape的_PresentationBuildTaskAssembly:

<_PresentationBuildTasksAssembly Condition="'$(_PresentationBuildTasksAssembly)'==''">$([MSBuild]::Unescape($([System.IO.Path]::GetFullPath('$(MSBuildThisFileDirectory)..tools$(_PresentationBuildTasksTfm)PresentationBuildTasks.dll'))))</_PresentationBuildTasksAssembly>

您也可以通过在Visual Studio中将构建更改为x64而不是任何CPU/x86来解决此问题。对于 64 位,这将使用C:Program Files而不是C:Program Files (x86),因此没有"坏"字符。

最新更新