You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在.NET MAUI类库中集成iOS原生绑定并创建NuGet包

解决方案:创建包含iOS原生绑定的.NET MAUI类库

问题核心

iOS原生绑定项目不需要添加net7.0、net7.0-android这类非iOS平台的目标框架,因为绑定代码(如ApiDefinitions.cs中的[Export]特性、iOS专属类型)仅能在iOS/Mac Catalyst平台编译,多目标框架会导致非iOS平台无法识别绑定语法,从而报错。


步骤1:修正iOS原生绑定项目配置

将绑定项目的目标框架限制为iOS和Mac Catalyst(若需支持Mac),并确保绑定资源仅在对应平台生效:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <!-- 仅保留iOS和Mac Catalyst目标框架 -->
    <TargetFrameworks>net7.0-ios;net7.0-maccatalyst</TargetFrameworks>
    <Nullable>enable</Nullable>
    <ImplicitUsings>true</ImplicitUsings>
    <IsBindingProject>true</IsBindingProject>
    <!-- 可选:设置Mac Catalyst最低支持版本 -->
    <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">13.1</SupportedOSPlatformVersion>
  </PropertyGroup>

  <ItemGroup>
    <ObjcBindingCoreSource Include="StructsAndEnums.cs" />
  </ItemGroup>
  <ItemGroup>
    <ObjcBindingApiDefinition Include="ApiDefinitions.cs" />
  </ItemGroup>

  <!-- 为iOS和Mac Catalyst添加原生框架引用 -->
  <ItemGroup Condition="'$(TargetFramework)' == 'net7.0-ios' OR '$(TargetFramework)' == 'net7.0-maccatalyst'">
    <NativeReference Include="..\..\..\..\..\Downloads\nativeSKD_iOS\NativeSDK.xcframework">
      <Kind>Framework</Kind>
      <Frameworks>Foundation</Frameworks>
      <ForceLoad>True</ForceLoad>
    </NativeReference>
  </ItemGroup>
</Project>

步骤2:调整MAUI类库项目配置

按目标框架条件引用对应原生项目,避免跨平台引用冲突:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFrameworks>net7.0;net7.0-android;net7.0-ios</TargetFrameworks>
        <UseMaui>true</UseMaui>
        <SingleProject>true</SingleProject>
        <ImplicitUsings>enable</ImplicitUsings>

        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">16.1</SupportedOSPlatformVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
    </PropertyGroup>

    <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net7.0-ios|AnyCPU'">
        <CreatePackage>false</CreatePackage>
    </PropertyGroup>

    <!-- Android专属依赖:仅在Android目标下引用 -->
    <ItemGroup Condition="'$(TargetFramework)' == 'net7.0-android'">
        <ProjectReference Include="..\NativeSdk.Android\NativeSdk.Android.csproj" />
        <AndroidAarLibrary Include="Platforms\Android\Aar\TransaktSDK.aar">
            <AndroidSkipResourceProcessing></AndroidSkipResourceProcessing>
        </AndroidAarLibrary>
    </ItemGroup>

    <!-- iOS专属依赖:仅在iOS目标下引用 -->
    <ItemGroup Condition="'$(TargetFramework)' == 'net7.0-ios'">
        <ProjectReference Include="..\NativeSdk.iOS\NativeSdk.iOS.csproj" />
        <ProjectReference Include="Path\To\Your\iOSNativeBindingProject.csproj" />
    </ItemGroup>

    <ItemGroup>
        <Folder Include="Platforms\Android\Models\" />
        <Folder Include="Platforms\Android\Aar\" />
    </ItemGroup>

    <ItemGroup>
        <Compile Condition=" '$(EnableDefaultCompileItems)' == 'true' " Update="SecureServiceInfo.cs">
            <ExcludeFromCurrentConfiguration>true</ExcludeFromCurrentConfiguration>
        </Compile>
    </ItemGroup>
</Project>

关键说明

  1. 绑定项目目标框架限制:IsBindingProject属性仅在iOS/Mac Catalyst平台生效,绑定代码依赖iOS runtime特性,其他平台无法编译。
  2. 条件引用:MAUI类库中通过Condition确保各平台仅引用对应原生项目,避免跨平台编译错误。
  3. xcframework兼容性:多数iOS原生xcframework已支持Mac Catalyst,因此可将其同时应用到两个目标框架。

内容的提问来源于stack exchange,提问作者helper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 19:57:48