![](/img/trans.png)
[英]Microsoft.Build.Evaluation.Project add folder and file force refresh of project in VS
[英]Why does loading a Xamarin Android .csproj into a Microsoft.Build.Evaluation.Project throw InvalidPropectFileException due to missing .targets file?
我正在嘗試使用Microsoft.Build.Evaluation.Project
的實例在 .NET 5.0 控制台應用程序中打開 Xamarin Android.csproj 文件。 此外,我還嘗試對 .NET Framework 4.6.1 項目執行相同的操作。
加載代碼為:
var fileName = @"C:\Users\vchel\source\repos\BlankAndroid\BlankAndroid\BlankAndroid.csproj";
var project = new global::Microsoft.Build.Evaluation.Project(fileName, null, null, new ProjectCollection());
當我嘗試實例化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
上面錯誤中引用的 Xamarin.Android.CSharp.targets 文件出現在我正在加載的 .csproj 中,如以下代碼片段所示:
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
請注意,.csproj 引用帶有$(MSBuildExtensionPath)
前綴的 Xamarin.Android.CSharp.targtets 文件,但在運行時,它與正在運行的控制台 application.exe 的位置交換,而不是
我嘗試加載的 Android 項目在 Visual Studio 中正確打開並且編譯/運行沒有問題。
為什么這個 Android 項目在 Visual Studio 中打開沒有任何問題,但在Microsoft.Build.Evaluation.Project
object 中嘗試打開時卻失敗了?
看來我之前確實遇到過同樣的問題,但它不在 Android 項目中,所以我將把這個問題留在這里以解決特定於 Android 的問題。
一般的問題在這里討論:
如何使用 MSBuild NuGet 將.csproj 文件加載到 .NET Core 3.0
總結是——.target 文件與正在安裝的 nuget 包是分開的,而且項目 class 似乎無法解析 $(MSBuildExtensionsPath)。 因此,要解決此問題,應將相關的.targets文件添加到項目中並復制到output(bin)文件夾中。
在我的機器上,可以在以下位置找到原始文件:
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Xamarin\Android
我必須將以下文件添加到我的應用程序中。 請注意,它們已添加到Xamarin/Android/
文件夾中:
如果較新,項目可以標記為復制:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.