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

.NET MAUI模板项目执行dotnet build -c Release --no-restore构建失败

MAUI构建失败问题分析与解决(Windows环境GitHub Actions适配)

命令行为差异的原因

  • dotnet build 会自动执行隐式restore,并且会根据当前Windows环境自动过滤不兼容的目标框架(比如maccatalyst-arm64),只还原当前环境能处理的框架资产,所以构建能正常完成。
  • 单独执行dotnet restore时,默认会还原所有目标框架的资产,但Windows环境下没有maccatalyst的构建工具链,导致maccatalyst-arm64的资产无法正常生成;后续执行dotnet build -c Release --no-restore时,构建系统会检查所有目标框架的资产,发现缺失就报错。
  • XAML自动生成类的过时警告:单独restore+build的流程会触发完整的代码生成逻辑,而dotnet build隐式restore时会跳过部分冗余生成步骤,因此警告不会触发。

解决方法(适配GitHub Actions Windows环境)

  • 指定restore和build的目标框架:只针对Windows相关框架操作,避免处理不兼容的maccatalyst资产:
    # 仅还原Windows框架依赖
    dotnet restore -f net10.0-windows10.0.19041.0
    # 仅构建Windows版本
    dotnet build -c Release --no-restore -f net10.0-windows10.0.19041.0
    
  • 修改项目文件排除冗余框架:如果不需要构建maccatalyst版本,直接在.csproj文件中删除net10.0-maccatalyst对应的TargetFrameworks条目,这样restore和build都会只处理Windows框架。
  • GitHub Actions工作流示例:在Windows runner中明确指定目标框架,避免跨环境框架检查:
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Setup .NET
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: 8.x # 对应VS2026配套的.NET版本
      - name: Restore Windows dependencies
        run: dotnet restore -f net10.0-windows10.0.19041.0
      - name: Build Windows release
        run: dotnet build -c Release --no-restore -f net10.0-windows10.0.19041.0
    
  • 处理XAML过时警告:如果警告不影响功能,可在.csproj中添加配置屏蔽:
    <PropertyGroup>
      <NoWarn>$(NoWarn);CS0618</NoWarn>
    </PropertyGroup>
    
    若需彻底解决,可检查XAML自动生成类的代码,替换为非过时的API实现。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.11 10:03:26