如何通过命令行发布Windows Application Package Project并支持ARM64
如何通过命令行发布UWP包?
我有一个使用Windows Application Package Project打包的WPF应用,最终要发布到微软商店。
为了通过商店审核,WPF项目需要使用我命名为“UWP”的预处理器指令编译,禁用商店应用不允许的功能。
我正在将WPF应用从.NET Framework 4.8迁移到.NET 5,同时希望通过命令行简化发布流程。
是否有办法配置或更新包以支持ARM64?
该应用的非商店版本会发布x86、x64和ARM64三个架构,我希望商店版本也保持一致。
查看WAPP时我发现ARM64不在可选架构列表中。如截图所示,我可以创建ARM64构建配置,但架构没有出现在列表中。
我的尝试(命令行问题)
我尝试运行以下命令:
MSBuild.exe C:\Project-UWP\Project.UWP\Project.UWP.wapproj /p:DefineConstants=UWP /p:Configuration=Release;AppxBundle=Always;AppxBundlePlatforms="x86|x64|ARM64" /p:OutDir="..\Publish\UWP"
但收到以下报错:
Microsoft (R) Build Engine version 16.11.0+0538acc04 for .NET Framework Copyright (C) Microsoft Corporation. All rights reserved. Build started 29/09/2021 22:09:19. Project "C:\Project-UWP\Project.UWP\Project.UWP.wapproj" on node 1 (default targets). C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(1718,5): warning NU1702: ProjectReference 'C:\Project\Project.csproj' was resolved using '.NETCoreApp,Version=v6.0' instead of the project target framework '.NETFramework,Version=v4.5.1'. This project may not be fully compatible with your project. [C:\Project-UWP\Project.UWP\Project.UWP.wapproj] C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(1718,5): warning NU1702: ProjectReference 'C:\Project\Project.csproj' was resolved using '.NETCoreApp,Version=v6.0' instead of the project target framework '.NETFramework,Version=v4.5.1'. This project may not be fully compatible with your project. [C:\Project-UWP\Project.UWP\Project.UWP.wapproj] _ResolveVCLibDependencies: Searching for SDKs targeting "UAP, 10.0.20348.0". Project "C:\Project-UWP\Project.UWP\Project.UWP.wapproj" (1) is building "C:\Project\Project.csproj" (2:9) on node 1 (publish target(s)). C:\Program Files\dotnet\sdk\5.0.401\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(241,5): error NETSDK1047: Assets file 'C:\Project\Project\obj\project.assets.json' doesn't have a target for 'net5.0-windows/win-x86'. Ensure that restore has run and that you have included 'net5.0-windows' in the TargetFrameworks for your project. You may also need to include 'win-x86' in your project's RuntimeIdentifiers. [C:\Project\Project.csproj] Done Building Project "C:\Project\Project.csproj" (publish target(s)) -- FAILED. Done Building Project "C:\Project-UWP\Project.UWP\Project.UWP.wapproj" (default targets) -- FAILED. Build FAILED. "C:\Project-UWP\Project.UWP\Project.UWP.wapproj" (default target) (1) -> (_GetProjectReferenceTargetFrameworkProperties target) -> C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(1718,5): warning NU1702: ProjectReference 'C:\Project\Project.csproj' was resolved using '.NETCoreApp,Version=v6.0' instead of the project target framework '.NETFramework,Version=v4.5.1'. This project may not be fully compatible with your project. [C:\Project-UWP\Project.UWP\Project.UWP.wapproj] C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(1718,5): warning NU1702: ProjectReference 'C:\Project\Project.csproj' was resolved using '.NETCoreApp,Version=v6.0' instead of the project target framework '.NETFramework,Version=v4.5.1'. This project may not be fully compatible with your project. [C:\Project-UWP\Project.UWP\Project.UWP.wapproj] "C:\Project-UWP\Project.UWP\Project.UWP.wapproj" (default target) (1) -> "C:\Project\Project.csproj" (publish target) (2:9) -> (ResolvePackageAssets target) -> C:\Program Files\dotnet\sdk\5.0.401\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(241,5): error NETSDK1047: Assets file 'C:\Project\obj\project.assets.json' doesn't have a target for 'net5.0-windows/win-x86'. Ensure that restore has run and that you have included 'net5.0-windows' in the TargetFrameworks for your project. You may also need to include 'win-x86' in your project's RuntimeIdentifiers. [C:\Project\Project.csproj] 2 Warning(s) 1 Error(s) Time Elapsed 00:00:01.31
注意存在和框架引用相关的警告:
ProjectReference C:\Project\Project.csproj was resolved using .NETCoreApp,Version=v6.0 instead of the project target framework .NETFramework,Version=v4.5.1. This project may not be fully compatible with your project. Microsoft.Common.CurrentVersion.targets 1719
WPF项目已经设置为使用.NET 5,我不清楚为什么会出现.NETCoreApp 6和.NET Framework 4.5.1的引用。
我的尝试(ARM64缺失问题)
我从零新建了一个WAPP,发现架构列表中出现了ARM64选项。
但通过右键菜单手动发布时,我收到以下报错:
The specified RuntimeIdentifier 'win-ARM64' is not recognized.
解决方案
一、修复命令行发布报错
- 调整WPF项目配置
打开WPF项目的.csproj文件,确认目标框架配置正确,同时添加所有需要的运行时标识符:
<PropertyGroup> <TargetFramework>net5.0-windows</TargetFramework> <RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers> <!-- 保留原有其他配置项 --> </PropertyGroup>
- 修复WAPP项目框架兼容警告
打开.wapproj项目文件,修改最低目标平台版本,同时添加兼容配置消除框架不匹配警告:
<PropertyGroup> <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion> <AssetTargetFallback>net5.0-windows</AssetTargetFallback> </PropertyGroup>
旧WAPP项目默认的目标框架是.NET Framework 4.5.1,这是出现NU1702警告的原因,添加以上配置后即可消除该警告。
3. 调整发布命令
需要先执行依赖还原操作,再执行打包发布,正确命令顺序如下:
首先还原所有架构的依赖:
MSBuild.exe C:\Project-UWP\Project.UWP\Project.UWP.wapproj /t:Restore /p:Configuration=Release /p:RuntimeIdentifiers=win-x86;win-x64;win-arm64
再执行打包:
MSBuild.exe C:\Project-UWP\Project.UWP\Project.UWP.wapproj /p:DefineConstants=UWP /p:Configuration=Release /p:AppxBundle=Always /p:AppxBundlePlatforms="x86|x64|arm64" /p:OutDir="..\Publish\UWP" /p:AppxPackageDir="..\Publish\UWP"
二、修复ARM64支持问题
- 确认WAPP项目版本配置
只有当WAPP项目的TargetPlatformMinVersion高于等于10.0.17763.0时,才会显示ARM64架构选项,旧项目没有该选项就是因为最低版本设置过低,按上文中的方式修改配置即可。 - 统一运行时标识符大小写
.NET的运行时标识符(RID)是大小写敏感的,必须使用小写的win-arm64,不要使用大写的win-ARM64,否则会出现无法识别的报错。 - 检查构建配置
打开Visual Studio的配置管理器,确认ARM64平台已经添加到WPF和WAPP项目的构建配置中,且两个项目的ARM64配置都勾选了生成选项。
内容的提问来源于stack exchange,提问作者Nicke Manarin
相关产品推荐
相关产品推荐

