【问题标题】:Conditionally set shortcut arguments in wix installer script在 wix 安装程序脚本中有条件地设置快捷方式参数
【发布时间】:2018-07-12 03:47:30
【问题描述】:

我有以下 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) 不为空时有条件地设置参数?

【问题讨论】:

    标签: wix installation shortcut wix3.11


    【解决方案1】:

    根据变量,使用preprocessor 有条件地编译带有或不带有Arguments 属性的Shortcut 元素。

    <?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>
    

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

    <?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 属性不太可能改变,所以我保留它以提高可读性。

    【讨论】:

    • 从未尝试在属性级别应用预处理器。奇怪的是它不起作用,我认为预处理涉及基本的字符串替换?我想这些结构是模式的一部分。我认为它 也失败了?
    • @SteinÅsmul WiX 预处理器在比简单的字符串替换更高的级别上工作。它首先使用符合标准的 XML 解析器读取 XML,当 processing instruction 出现在元素的属性部分中时,该解析器会阻塞。只有在那之后,它才会进行实际的预处理工作。
    • 是的,根据您之前的解释,我想了很多,感谢您的验证。要测试的东西太多,什么都尝试。
    • 我不得不将其更改为 ,因为即使它为空,它仍然是“定义的”。感谢您的回答,在我尝试#if #else 等但没有任何运气之后,我错误地认为没有预处理器。
    猜你喜欢
    • 2010-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多