【问题标题】:How to pass parameters to the custom action?如何将参数传递给自定义操作?
【发布时间】:2015-01-21 23:21:01
【问题描述】:

我正在尝试使用“值”属性创建自定义操作,我想将参数传递给 C# 代码(TARGETDIR 和版本)。

但是,我收到一条错误消息,指出 DLLENtry 和 Value 不能共存。但是没有dllentry的自定义动作是无效的。

这是代码:

 <CustomAction Id="SetMAWPrefferences"
                Value="InstallDir=[TARGETDIR];Version=2.0.0.1"
                Return="check"
                Execute="commit"
                BinaryKey="ImportExportBinary"                    
                />

为此我收到此错误:

错误 9 ICE68:操作“SetMAWPrefferences”的自定义操作类型无效。

有什么办法吗?

【问题讨论】:

    标签: c# wix custom-action


    【解决方案1】:

    注意,您以错误的方式使用 Value 属性:

    ...此属性必须与Property 属性一起使用才能设置属性...Source


    根据Creating WiX Custom Actions in C# and Passing Parameters 文章,您应该:

    1. 创建具有所需值的属性:

      <Property Id="InstallDir" Value="someDefaultValue" />
      <Property Id="Version" Value="2.0.0.1" />
      
    2. 创建自定义操作以设置InstallDir 属性:

      <CustomAction Id="SetDirProp" Property="InstallDir" Value="[TARGETDIR]" />
      
    3. 创建自定义操作:

      <CustomAction Id="SetMAWPrefferences" 
          Return="check" 
          Execute="commit" 
          BinaryKey="ImportExportBinary" 
          DllEntry="YourCustomAction" />
      
    4. 在安装过程中安排执行自定义操作:

      <InstallExecuteSequence>
          <Custom Action="SetDirProp" After="CostFinalize" />
          <Custom Action="SetMAWPreferences" ... />
          ...
      </InstallExecuteSequence>
      
    5. 从您的自定义操作中访问这些属性,如下所示:

      [CustomAction]
      public static ActionResult YourCustomAction(Session session)
      {
          // session["InstallDir"]
          // session["Version"]
      }
      

    【讨论】:

    • 嗨,值没有通过 - 我得到空值。
    • 谢谢你,价值观通过了!但是,我得到 [TARGETDIR] 作为字符串,而 google 没有帮助 - 我需要路径,而不是属性的名称。
    • @user1223457,我再次更新了答案,希望对您有所帮助。
    • 不,我仍然只得到一个字符串“TARGETDIR”
    • Execute="commit"............没有。 Commit CA 在InstallFinalize 之后执行,并且仅在启用回滚的情况下执行。如果要设置属性,则需要在 immediate CA 中完成,如果要更改目标系统,则需要在 deferred CA 中完成。 Commit CA 用于在安装成功后清理临时文件。
    【解决方案2】:

    有两种方法可以将参数传递给自定义操作,一种适用于立即执行的 CA,另一种适用于延迟的自定义操作。

    立即 CA(不能回滚):

    为了将参数传递给直接 CA,您可以设置具有所需名称的属性并从会话中访问它。

    在 Wix 中:

    <Property Id="MyProp" Value="MyValue" />
    

    在加州:

    [CustomAction]
    public static ActionResult NameOfMyCA(Session session)
    {
        string myArg = session["MyProp"];
    }   
    

    延迟 CA:

    要将参数传递给延迟 CA,您需要使用 CustomActionData Property,此属性是您可以从延迟 CA 访问的唯一属性。

    对于 WIX,DTF 包含一个 CustomActionData 类,它是一个键/值字典,您可以使用以下方式访问它:

    在 Wix 中:

    <CustomAction Id="MyCustomAction" .../>
    
    <Property Id="MyCustomAction" Value="Arg1=value1;Arg2=value2;Arg3=value3;Arg4=[MyProperty]" />
    

    在加州:

    [CustomAction]
    public static ActionResult NameOfMyCA(Session session)
    {
        CustomActionData data = session.CustomActionData;
    
        //Access each argument like this:
    
        string arg1 = data["Arg1"];
        string arg2 = data["Arg2"];
        string arg3 = data["Arg3"];
    }    
    

    立即 CA + CustomActionData:

    如果您想为您的即时 CA 使用 CustomActionData,您可以执行以下操作:

    在 Wix 中:

    <Property Id="MyCustomAction" Value="Arg1=value1;Arg2=value2;Arg3=value3;Arg4=[MyProperty]" />
    

    在加州:

    [CustomAction]
    public static ActionResult NameOfMyCA(Session session)
    {
        CustomActionData data = new CustomActionData(session["MyCustomAction"]);
    
        //Access each argument like this:
    
        string arg1 = data["Arg1"];
        string arg2 = data["Arg2"];
        string arg3 = data["Arg3"];
        string arg4 = session.Format(data["Arg4"]);
    }
    

    对于 Arg4,因为它包含属性的值,您需要像这样访问它:

    string arg4 = session.Format(data["Arg4"]);
    

    不幸的是,这仅适用于直接 CA,这意味着如果您想在延迟 CA 中使用此属性的值,您将需要有两个自定义操作:

    • CA 1 将 CA 的 CustomActionData 设置为立即执行。 (请记住使用为您的 CustomAction 定义的相同名称来命名属性。

    • CA 2 具有使用 CustomActionData 的特定逻辑的 CA。

    我建议您对所有情况都使用 CustomActionData,这样更容易将您的 CA 从 Immediate 转换为 Deferred,并且代码更易于阅读。

    参考资料:

    session.Format CustomActionData

    【讨论】:

    • 我认为这个答案缺少实际设置属性的 ,例如&lt;CustomAction Id="SetDirProp" Property="InstallDir" Value="[TARGETDIR]" /&gt;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    相关资源
    最近更新 更多