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

如何无需新建构建配置为.NET MAUI Windows版创建条件编译符号

实现.NET MAUI Windows多渠道构建方案

一、定义渠道专属条件编译符号

在项目的.csproj文件中,通过PropertyGroup结合平台和构建配置,动态注入渠道对应的编译符号,无需新增额外构建配置:

<Project Sdk="Microsoft.NET.Sdk">
  <!-- 保留原有项目配置 -->

  <!-- Windows平台渠道编译符号注入 -->
  <PropertyGroup Condition="'$(TargetPlatform)' == 'Windows' and '$(Channel)' != ''">
    <DefineConstants>$(DefineConstants);$(Channel.ToUpper())</DefineConstants>
  </PropertyGroup>

  <!-- 渠道专属NuGet包依赖 -->
  <ItemGroup Condition="'$(Channel)' == 'Steam'">
    <PackageReference Include="Steamworks.NET" Version="20.1.0" />
  </ItemGroup>
  <ItemGroup Condition="'$(Channel)' == 'Epic'">
    <PackageReference Include="EpicOnlineServices" Version="1.15.0" />
  </ItemGroup>
  <ItemGroup Condition="'$(Channel)' == 'MicrosoftStore'">
    <PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.231008000" />
  </ItemGroup>
</Project>

二、代码中使用渠道条件编译

在Windows相关代码里,直接用定义好的符号包裹渠道专属逻辑:

#if STEAM
// Steam SDK初始化、成就同步等专属逻辑
SteamAPI.Init();
#endif

#if EPIC
// Epic在线服务初始化、云存储专属逻辑
EOS_Platform_Initialize();
#endif

#if MICROSOFTSTORE
// 微软商店许可证验证、内购专属逻辑
var licenseManager = new LicenseManager();
await licenseManager.CheckLicenseAsync();
#endif

三、构建管道中指定渠道参数

通过/p:Channel=XXX参数指定目标渠道,保留Debug/Release核心配置:

命令行构建示例

# 构建Windows Steam渠道Release版本
dotnet build -c Release -f net8.0-windows10.0.19041.0 /p:Channel=Steam

# 构建Windows微软商店渠道Release版本(自动触发打包)
dotnet publish -c Release -f net8.0-windows10.0.19041.0 /p:Channel=MicrosoftStore /p:PackageCertificateKeyFile=YourStoreCert.pfx

GitHub Actions构建片段

jobs:
  build-windows:
    runs-on: windows-latest
    strategy:
      matrix:
        channel: [Steam, Epic, MicrosoftStore]
    steps:
      - uses: actions/checkout@v4
      - name: Setup .NET
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: 8.0.x
      - name: Build ${{ matrix.channel }} Release
        run: dotnet build -c Release -f net8.0-windows10.0.19041.0 /p:Channel=${{ matrix.channel }}
      - name: Publish Microsoft Store Version
        if: matrix.channel == 'MicrosoftStore'
        run: dotnet publish -c Release -f net8.0-windows10.0.19041.0 /p:Channel=MicrosoftStore /p:PackageCertificateKeyFile=${{ secrets.STORE_CERT }}

四、渠道专属打包逻辑区分

通过Condition属性控制打包行为,实现不同渠道的打包差异:

<!-- 仅微软商店版本启用打包 -->
<PropertyGroup Condition="'$(Channel)' == 'MicrosoftStore'">
  <PublishProfile>win10-$(Configuration).pubxml</PublishProfile>
  <GenerateAppxPackageOnBuild>true</GenerateAppxPackageOnBuild>
</PropertyGroup>

<!-- Steam/Epic版本跳过打包,直接输出可执行文件 -->
<PropertyGroup Condition="'$(Channel)' == 'Steam' or '$(Channel)' == 'Epic'">
  <GenerateAppxPackageOnBuild>false</GenerateAppxPackageOnBuild>
  <SelfContained>true</SelfContained>
</PropertyGroup>

这套方案完全保留Debug和Release两个核心配置,通过Channel参数动态切换Windows端的渠道逻辑、依赖和打包行为,避免.csproj因多配置变得混乱,同时适配CI/CD管道的自动化构建需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 02:45:23