简体   繁体   English

为什么将 Xamarin Android.csproj 加载到 Microsoft.Build.Evaluation.Project 中会由于 missing.targets 文件而抛出 InvalidPropectFileException?

[英]Why does loading a Xamarin Android .csproj into a Microsoft.Build.Evaluation.Project throw InvalidPropectFileException due to missing .targets file?

I am attempting to open a Xamarin Android.csproj file in a .NET 5.0 console application using an instance of Microsoft.Build.Evaluation.Project .我正在尝试使用Microsoft.Build.Evaluation.Project的实例在 .NET 5.0 控制台应用程序中打开 Xamarin Android.csproj 文件。 Additionally I have also attempted to do the same with a .NET Framework 4.6.1 project.此外,我还尝试对 .NET Framework 4.6.1 项目执行相同的操作。

The code for loading is:加载代码为:

var fileName = @"C:\Users\vchel\source\repos\BlankAndroid\BlankAndroid\BlankAndroid.csproj";
var project = new global::Microsoft.Build.Evaluation.Project(fileName, null, null, new ProjectCollection());

When I attempt to instantiate the Project object, I get the following exception:当我尝试实例化Project object 时,出现以下异常:

Microsoft.Build.Exceptions.InvalidProjectFileException: 
The imported project "C:\Users\vchel\source\repos\ProjectLoadTest\ProjectLoadTest\bin\Debug\net5.0\Xamarin\Android\Xamarin.Android.CSharp.targets" was not found.
Confirm that the expression in the Import declaration "C:\Users\vchel\source\repos\ProjectLoadTest\ProjectLoadTest\bin\Debug\net5.0\Xamarin\Android\Xamarin.Android.CSharp.targets" is correct, and that the file exists on disk.  C:\Users\vchel\source\repos\BlankAndroid\BlankAndroid\BlankAndroid.csproj

The Xamarin.Android.CSharp.targets file referenced in the error above appears in the.csproj that I am loading as shown in the following snippet:上面错误中引用的 Xamarin.Android.CSharp.targets 文件出现在我正在加载的 .csproj 中,如以下代码片段所示:

  <Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />

Notice that the.csproj references the Xamarin.Android.CSharp.targtets file with the $(MSBuildExtensionPath) prefix, but at runtime this is swapped with the location of the running console application.exe rather than请注意,.csproj 引用带有$(MSBuildExtensionPath)前缀的 Xamarin.Android.CSharp.targtets 文件,但在运行时,它与正在运行的控制台 application.exe 的位置交换,而不是

The Android project which I am attempting to load opens correctly in Visual Studio and compiles/runs with no problems.我尝试加载的 Android 项目在 Visual Studio 中正确打开并且编译/运行没有问题。

Why does this Android project open without any problems in Visual Studio, yet it fails when attempting to open in a Microsoft.Build.Evaluation.Project object?为什么这个 Android 项目在 Visual Studio 中打开没有任何问题,但在Microsoft.Build.Evaluation.Project object 中尝试打开时却失败了?

It seems I've actually run into this same problem before, but it wasn't on an Android project so I'll keep this question here to address Android-specific issues.看来我之前确实遇到过同样的问题,但它不在 Android 项目中,所以我将把这个问题留在这里以解决特定于 Android 的问题。

The problem in general is discussed here:一般的问题在这里讨论:

How to Load.csproj file into .NET Core 3.0 using MSBuild NuGet 如何使用 MSBuild NuGet 将.csproj 文件加载到 .NET Core 3.0

The summary is - the.target files are separate from the nuget packages being installed, and it seems like the Project class can't resolve $(MSBuildExtensionsPath).总结是——.target 文件与正在安装的 nuget 包是分开的,而且项目 class 似乎无法解析 $(MSBuildExtensionsPath)。 Therefore, to solve this problem the relevant.targets files should be added to the project and copied to the output (bin) folder.因此,要解决此问题,应将相关的.targets文件添加到项目中并复制到output(bin)文件夹中。

On my machine, the original files can be found in:在我的机器上,可以在以下位置找到原始文件:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Xamarin\Android

I had to add the following files to my app.我必须将以下文件添加到我的应用程序中。 Notice they are added to the Xamarin/Android/ folder:请注意,它们已添加到Xamarin/Android/文件夹中:

在此处输入图像描述

Items can be marked as Copy if newer :如果较新,项目可以标记为复制

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Microsoft.Build.Evaluation.Project 在VS中添加文件夹和文件强制刷新项目 - Microsoft.Build.Evaluation.Project add folder and file force refresh of project in VS 从C#运行Microsoft.Build.Evaluation.Project的并行构建 - Run parallel build of Microsoft.Build.Evaluation.Project from C# 如何在MSBuild 14.0中重新加载Microsoft.Build.Evaluation.Project - How can I reload a Microsoft.Build.Evaluation.Project in MSBuild 14.0 使用 Microsoft.Build.Evaluation(而不是 Engine)以编程方式修改 csproj 文件 - Modify programatically csproj files with Microsoft.Build.Evaluation (instead of Engine) 为什么 do.net 构建在 .NET Framework 4.5 项目上会抛出“该项目引用了 NuGet 缺少的包”? - Why does dotnet build on a .NET Framework 4.5 project throw "This project references NuGet package(s) that are missing"? 将 a.csproj 的宏导入 Directory.build.targets 文件? - Importing macros of a .csproj to a Directory.build.targets file? 为什么Microsoft.Build.Framework.dll 15.3无法加载csproj,15.1是否加载? - Why does Microsoft.Build.Framework.dll 15.3 not load csproj, 15.1 does? 在.csproj中包含.targets文件以全局定义项目和MSBuild变量 - Including a .targets file in .csproj to globally define project & MSBuild variables 缺少“ Microsoft.CSharp.targets”文件 - “Microsoft.CSharp.targets” file is missing 弄清楚为什么 Microsoft.Build.Evaluation.Project.Build() 返回 false - Figure out why Microsoft.Build.Evaluation.Project.Build() returns false
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM