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

通过PowerShell修改Appxmanifest:添加指定XML元素

在Azure DevOps构建流水线中正确修改Package.appxmanifest添加rescap:Capability的方法

问题背景

搭建Azure DevOps构建流水线时,需要按需修改生成的Package.appxmanifest文件,目标是在<Capabilities>节点下添加<rescap:Capability Name="perceptionSensorsExperimental" />元素。

目标修改后的XML片段:

<Package>
  <Capabilities>
    <uap2:Capability Name="spatialPerception" />
    <rescap:Capability Name="perceptionSensorsExperimental" />
  </Capabilities>
</Package>

尝试了以下PowerShell代码:

$newElement = $manifest.Package.Capabilities.AppendChild($manifest.CreateElement("rescap:Capability"))
$newElement.SetAttribute(“Name”,”perceptionSensorsExperimental”)

但生成了错误的XML,元素丢失了rescap:前缀,且被添加了空的xmlns=""属性:

<Package>
  <Capabilities>
    <uap2:Capability Name="spatialPerception" />
    <Capability Name="perceptionSensorsExperimental" xmlns="" />
  </Capabilities>
</Package>

补充:完整的初始Package.appxmanifest文档如下:

<?xml version="1.0" encoding="utf-8"?>
<Package xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
         xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
         xmlns:uap2="http://schemas.microsoft.com/appx/manifest/uap/windows10/2"
         xmlns:uap3="http://schemas.microsoft.com/appx/manifest/uap/windows10/3"
         xmlns:uap4="http://schemas.microsoft.com/appx/manifest/uap/windows10/4"
         xmlns:iot="http://schemas.microsoft.com/appx/manifest/iot/windows10"
         xmlns:mobile="http://schemas.microsoft.com/appx/manifest/mobile/windows10"
         ignorableNamespaces="uap uap2 uap3 uap4 mp mobile iot rescap"
         xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
         xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities">
  <Identity Name="XXXX" Publisher="CN=XXXX" Version="1.0.0.0" />
  <Properties>
    <DisplayName>XXXX</DisplayName>
    <PublisherDisplayName>XXXXX</PublisherDisplayName>
    <Logo>Assets\StoreLogo.png</Logo>
  </Properties>
  <Dependencies>
    <TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.10240.0" MaxVersionTested="10.0.22621.0" />
  </Dependencies>
  <Resources>
    <Resource Language="x-generate" />
  </Resources>
  <Applications>
    <Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="XXXX.App">
      <uap:VisualElements DisplayName="XXXXX" Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png" Description="XXXX" BackgroundColor="transparent">
        <uap:DefaultTile ShortName="XXXX" Wide310x150Logo="Assets\Wide310x150Logo.png" />
        <uap:SplashScreen Image="Assets\SplashScreen.png" BackgroundColor="#FFFFFF" />
        <uap:InitialRotationPreference>
          <uap:Rotation Preference="landscape" />
          <uap:Rotation Preference="landscapeFlipped" />
          <uap:Rotation Preference="portrait" />
          <uap:Rotation Preference="portraitFlipped" />
        </uap:InitialRotationPreference>
      </uap:VisualElements>
    </Application>
  </Applications>
  <Capabilities>
    <Capability Name="internetClient" />
    <Capability Name="internetClientServer" />
    <Capability Name="privateNetworkClientServer" />
    <uap:Capability Name="sharedUserCertificates" />
    <uap2:Capability Name="spatialPerception" />
    <DeviceCapability Name="webcam" />
    <DeviceCapability Name="microphone" />
    <DeviceCapability Name="gazeinput" />
  </Capabilities>
</Package>

解决方案

问题根源是创建元素时未指定rescap前缀对应的命名空间URI,XML解析器无法识别前缀,因此自动添加了空的默认命名空间。

正确的PowerShell代码如下:

# 从根节点提取rescap命名空间的URI
$rescapNamespace = $manifest.DocumentElement.GetAttribute("xmlns:rescap")

# 创建带指定命名空间的rescap:Capability元素
$newElement = $manifest.CreateElement("rescap:Capability", $rescapNamespace)

# 设置Name属性
$newElement.SetAttribute("Name", "perceptionSensorsExperimental")

# 将元素添加到Capabilities节点下
$manifest.Package.Capabilities.AppendChild($newElement) | Out-Null

# 保存修改后的manifest(按需执行)
$manifest.Save("你的文件路径\Package.appxmanifest")

验证结果

修改后的XML片段将正确生成预期元素:

<Package>
  <Capabilities>
    <uap2:Capability Name="spatialPerception" />
    <rescap:Capability Name="perceptionSensorsExperimental" />
  </Capabilities>
</Package>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 05:05:41