Xamarin

重要

Visual Studio App Center 计划于 2025 年 3 月 31 日停用。 虽然可以继续使用 Visual Studio App Center,直到它完全停用,但有几种建议的替代方法可以考虑迁移到其中。

详细了解支持时间表和替代方案

我的 Xamarin.iOS 从解决方案文件(.sln)而不是项目文件(.csproj)生成生成

当 Xamarin.iOS 生成从解决方案文件(.sln)运行时,可能需要检查一些事项。

应在代码中禁用 Android 和 UWP 项目,以用于适用于 iOS 生成的生成配置。 转到解决方案的配置映射,对于面向 iPhone 和 iPhoneSimulator 的所有映射,请取消选中面向不同平台的所有项目。 此配置可确保在开始生成时 .sln ,不会尝试生成其他项目。

我的 Xamarin.iOS 版本无法声明我需要提供签名信息

如果 Xamarin.iOS 版本未签名,但生成过程需要签名,则可能是因为已在 Sign builds: Off App Center 分支配置中选择。

如果生成日志包含:

RequireProvisioningProfile: True. 

这意味着项目本身已配置为签名,尽管应用中心配置,但应用签名。

若要修复此问题,请在 IDE 中打开项目选项>生成 > iOS 捆绑包签名,并确保项目配置(例如 Debug|iPhoneSimulator)不包含除“自动”以外的任何签名信息。

我的 Xamarin.Android 生成失败,出现 错误:找不到 APK 文件

在 Xamarin Android Postprocess 任务期间生成失败的一个常见原因是 Android 项目文件中的属性值<OutputPath>不正确。 若要检查它,请转到 Xamarin.Android > 项目选项 > 生成 > 输出 ,并验证生成配置(调试/发布)是否指向默认位置。 通常,它应该是 YourProjectDir/bin/$(Configuration)

我设置了 Xamarin.iOS 应用分支以在不签名的情况下进行生成,但生成失败,声称我需要提供签名信息

如果在 Sign builds: Off App Center 分支配置中选择,并且生成日志包含 RequireProvisioningProfile: True,则意味着项目本身已配置为签名,并且会尝试应用签名,尽管应用了 App Center 配置。 若要修复此问题,请在 IDE 中打开“生成 > iOS 捆绑包签名”,>并确保项目配置(例如 Debug|iPhoneSimulator)不包含除“自动”以外的任何签名信息。

在 Xamarin.iOS 应用程序中禁用调试配置的签名

我的 Xamarin.iOS 模拟器生成无法安装到 iOS 模拟器中,出现“无法 chmod ... /Appname.iOS.app/Appname.iOS:没有此类文件或目录”错误。

在 Visual Studio 中创建 Xamarin.iOS 项目时,iPhoneSimulator 的默认配置具有 i386 + x86_64 支持的体系结构。 从此类配置生成的.app文件将无法上传到模拟器中。 打开项目选项>生成 > iOS 生成和 iPhoneSimulator 配置将支持的体系结构更改为 i386x86_64

在 Xamarin.iOS 应用程序中的 iPhoneSimulator 配置支持的体系结构中设置x86_64

我的 Xamarin 生成失败并出现错误MSB4018:“WriteRestoreGraphTask”任务意外失败。

解决方案与较新的 .NET Standard 项目一起包含 PCL 或较旧的 .NET Standard 项目。 这意味着它们可能包含文件中的引用 PackageTargetFallbackAssetTargetFallback 引用 .csproj 。 生成日志还将包含如下所示的消息:

error MSB4018: NuGet.Commands.RestoreCommandException: PackageTargetFallback and AssetTargetFallback cannot be used together. Remove PackageTargetFallback(deprecated) references from the project environment.

若要解决此问题,请删除 PackageTargetFallback(往往位于较旧的 PCL .csproj 文件中),或将其重命名为 AssetTargetFallback? 此 StackOverflow 线程中还介绍了该解决方案。

我的 Xamarin 生成失败并出现错误:此项目引用此计算机上缺少的 NuGet 包。

看起来并非所有包都已还原以生成应用程序。 生成日志还将包含以下消息:

warning MSB3245: Could not resolve this reference. Could not locate the assembly "ASSEMBLY_NAME". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
error CS0246: The type or namespace name 'TYPE_OR_NAMESPACE_NAME' could not be found (are you missing a using directive or an assembly reference?)

若要解决此问题,可以使用以下命令使用预生成脚本appcenter-pre-build.sh,这将还原存储库中每个解决方案的所有包:

#!/bin/bash
find $APPCENTER_SOURCE_DIRECTORY -name '*.sln' -print0 | xargs -0 -n1 nuget restore -DisableParallelProcessing

我想为 Xamarin 应用程序运行单元测试

若要在 Xamarin 生成中运行单元测试,请使用 生成后脚本。 例如,当基于 NUnit 的项目名称中具有 “测试 ”时,可以使用以下脚本来生成、运行和显示结果:

echo "Found NUnit test projects:"
find $APPCENTER_SOURCE_DIRECTORY -regex '.*Test.*\.csproj' -exec echo {} \;
echo
echo "Building NUnit test projects:"
find $APPCENTER_SOURCE_DIRECTORY -regex '.*Test.*\.csproj' -exec msbuild {} \;
echo
echo "Compiled projects to run NUnit tests:"
find $APPCENTER_SOURCE_DIRECTORY -regex '.*bin.*Test.*\.dll' -exec echo {} \;
echo
echo "Running NUnit tests:"
find $APPCENTER_SOURCE_DIRECTORY -regex '.*bin.*Test.*\.dll' -exec nunit3-console {} \;
echo
echo "NUnit tests result:"
find . -name 'TestResult.xml' -exec cat {} \;

我收到错误:找不到任何项目,也没有找到 Xamarin 生成的配置

它可能是存储库深度的问题, .csproj 并且 .sln 位于其中。 由于性能原因,当前分析器存在限制。

对于 .csproj 文件,它不应低于包含存储库根目录的四个目录。

对于 .sln 文件,它不应低于包含存储库根目录的两个目录。

如何实现还原专用 NuGet 源?

如果 NuGet.Config 文件已签入到存储库中,并位于.sln或存储库的根级别旁边,则 App Center 会在添加专用 NuGet 源时还原它们,如以下示例所示。 可以使用环境变量安全地添加凭据。

对于 Mac 生成计算机:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget" value="https://api.nuget.org/v3/index.json" />
    <add key="MyGet" value="https://www.myget.org/F/MyUsername/api/v2/index.json" />
    <add key="MyAuthNuget" value="https://nuget.example.com/v2/index.json" />
  </packageSources>
  <activePackageSource>
    <add key="All" value="(Aggregate source)" />
  </activePackageSource>
  <packageSourceCredentials>
    <MyAuthNuget>
      <add key="Username" value="%USER_VARIABLE%" />
      <add key="ClearTextPassword" value="%PASSWORD_VARIABLE%" />
    </MyAuthNuget>
  </packageSourceCredentials>
</configuration>

对于 Windows 生成计算机,请参阅 UWP C#

如果配置复杂且需要更多信息,可以参考 “配置 NuGet”行为

Builds 卡在 CompileToNative

如果生成遇到与此 GitHub 问题中所述的类似症状,请尝试仅通过添加以下参数来生成 ARM64,如问题中所示:

<MtouchArch>ARM64</MtouchArch>

生成失败,出现错误:项目中不存在目标“_IsProjectRestoreSupported”。

如果在解决方案中有 UWP 项目,在还原期间其错误在旧版 NuGet 中以无提示方式忽略,则可能会遇到生成问题。 删除或修复解决方案中的此类 UWP 项目可能会解决问题。 Plese 在此 GitHub 问题中看到详细信息