首页 > 解决方案 > 在 wix 安装程序脚本中有条件地设置快捷方式参数

问题描述

我有以下 WIX 快捷方式。

 <!--Desktop shortcuts-->
  <Directory Id="DesktopFolder" Name="Desktop">
    <Component Id="CMP_DesktopShortcuts" Guid="{guidblah}">
      <Shortcut Id="Shotcut_Editor_Desktop"
                Name ="Software"
                Description="Software Description"
                Arguments="$(var.CmdLineArgs)"
                Target="blah.exe">
      </Shortcut>

      <RegistryValue Root="HKCU"
                      Key="Software\blah"
                      Name="DesktopShortcutInstalled"
                      Type="integer"
                      Value="1"
                      KeyPath="yes"
      />
    </Component>
  </Directory>

我根据我正在构建的安装程序在我的构建脚本中设置 CmdLineArgs。我的一个构建脚本没有命令行参数,因此将 CmdLineArgs 设置为 null。

然后我得到这个错误:

错误 CNDL0006:Shortcut/@Arguments 属性的值不能为空字符串。如果不需要值,只需删除整个属性。

仅当 $(var.CmdLineArgs) 不为空时,如何有条件地设置参数?

标签: wixinstallationshortcutwix3.11

解决方案


使用预处理器根据变量有条件地编译Shortcut带有或不带有属性的元素。Arguments

<?ifdef CmdLineArgs?>
    <Shortcut Id="Shotcut_Editor_Desktop"
            Name ="Software"
            Description="Software Description"
            Arguments="$(var.CmdLineArgs)"
            Target="blah.exe">
    </Shortcut>
<?else?>
    <Shortcut Id="Shotcut_Editor_Desktop"
            Name ="Software"
            Description="Software Description"
            Target="blah.exe">
    </Shortcut>
<?endif?>

不幸的是,这里有一些重复,因为预处理器条件不能应用于属性级别,元素是最小的粒度。

以下是无效的XML:

<Shortcut Id="Shotcut_Editor_Desktop"
        Name ="Software"
        Description="Software Description"
    <?ifdef CmdLineArgs?>
        Arguments="$(var.CmdLineArgs)"
    <?endif?>
        Target="blah.exe">
</Shortcut>

您也可以通过对其他属性使用预处理器变量来消除一些重复,例如:

<?ifdef CmdLineArgs?>
    <Shortcut Id="Shotcut_Editor_Desktop"
            Name ="$(var.ProductName)"
            Description="$(var.ProductDescription)"
            Arguments="$(var.CmdLineArgs)"
            Target="$(var.ProductExeFile)">
    </Shortcut>
<?else?>
    <Shortcut Id="Shotcut_Editor_Desktop"
            Name ="$(var.ProductName)"
            Description="$(var.ProductDescription)"
            Target="$(var.ProductExeFile)">
    </Shortcut>
<?endif?>

Id属性不太可能改变,所以我保留它是为了更好的可读性。


推荐阅读