无需创建完整appinstaller文件,如何修改MSIX自动更新OnLaunch HoursBetweenUpdateChecks值?
核心问题:无需完整重写appinstaller修改HoursBetweenUpdateChecks的方法
你不需要手动维护完整的appinstaller文件,有两种更高效的方式实现需求:
1. 自定义appinstaller模板并关联到发布配置
在项目中添加一个自定义的AppInstallerTemplate.xml文件,保留自动生成的结构,仅修改HoursBetweenUpdateChecks为0:
<?xml version="1.0" encoding="utf-8"?> <AppInstaller xmlns="http://schemas.microsoft.com/appx/appinstaller/2018" Version="1.0.0.0" Uri="http://your-publish-location/YourApp.appinstaller"> <MainBundle Name="com.yourcompany.yourapp" Publisher="CN=YourPublisher" Version="$(BundleVersion)" Uri="http://your-publish-location/YourApp_$(BundleVersion)_Test/Package/YourApp_$(BundleVersion)_x64.msixbundle" /> <UpdateSettings> <OnLaunch HoursBetweenUpdateChecks="0" /> <AutomaticBackgroundTask /> </UpdateSettings> </AppInstaller>
然后在你的发布配置文件(.pubxml)中添加以下属性,指定使用这个模板生成最终的appinstaller:
<PropertyGroup> <AppInstallerTemplatePath>AppInstallerTemplate.xml</AppInstallerTemplatePath> </PropertyGroup>
后续每次发布时,Visual Studio会基于这个模板填充动态内容(如版本号、发布URI),同时保留你设置的HoursBetweenUpdateChecks值。
2. 使用MSBuild目标动态修改生成的appinstaller
如果不想维护模板,可以直接在.pubxml中添加MSBuild任务,在自动生成appinstaller后自动修改目标属性:
<Target Name="OverrideUpdateCheckInterval" AfterTargets="GenerateAppInstallerFile"> <XmlPoke XmlInputPath="$(AppInstallerOutputPath)" Query="/AppInstaller/UpdateSettings/OnLaunch/@HoursBetweenUpdateChecks" Value="0" /> </Target>
这个任务会在发布流程生成appinstaller文件后,自动将HoursBetweenUpdateChecks的值改为0,完全无需手动干预。
你的三个疑问解答
1. 为什么Visual Studio/MAUI不生成可编辑的appinstaller文件?
appinstaller文件包含大量动态生成的内容(如包版本、依赖项清单、发布URI等),如果生成静态可编辑文件,每次发布时这些动态内容需要手动同步,极易引发配置错误。Visual Studio的设计思路是让用户通过发布配置(.pubxml)控制生成逻辑,而非直接编辑最终的部署清单。不过你可以通过上面的自定义模板方式,实现对特定属性的持久化修改。
2. 为什么无法在.pubxml中直接定义这类“简单”属性?
.pubxml的核心职责是定义发布流程的参数(如发布位置、签名证书、版本生成规则等),而appinstaller中的更新设置属于应用部署的运行时规则,两者的设计边界不同。MSIX的appinstaller规范属性众多,Visual Studio并未将所有属性都映射为pubxml的内置参数,但通过自定义MSBuild目标或模板,完全可以实现对这些属性的控制。
3. 为何同时存在.pubxml和.appinstaller?
两者的定位完全不同:
.pubxml是Visual Studio的发布配置文件,用于控制构建和发布过程的行为,比如是否启用自动更新、发布到哪个服务器、如何生成版本号等。.appinstaller是MSIX的部署清单文件,是Windows部署引擎识别的标准格式,用于告知系统如何检查更新、获取包文件、处理依赖项等运行时逻辑。
它们分工明确:前者管“怎么发布”,后者管“怎么部署和更新”。
内容的提问来源于stack exchange,提问作者Rand Random

