问题描述
如何创建自定义操作并将其链接到我的 WiX 设置项目?
我有:
How do I create custom actions and link it to my WiX setup project?
I have:
- WiX 3.11
- Visual Studio
推荐答案
您需要在您的解决方案中创建一个新的 C# Custom Action Project for WiX v3.
应该是这样的:
You need to create a new C# Custom Action Project for WiX v3 in your solution.
That should look like this:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Deployment.WindowsInstaller;
namespace CustomAction
{
public class CustomActions
{
[CustomAction]
public static ActionResult CustomAction(Session session)
{
session.Log("Begin CustomAction");
return ActionResult.Success;
}
}
}
将函数名称更改为适合您的函数的名称.
之后,右键单击 WiX 设置项目上的 References 文件夹,然后选择 Add Reference....
点击标签 Projects 并选择您的自定义操作 Project.
在最后一步中,您需要将此代码添加到您的 Product.wxs:
Change the function name to a name that fits your function.
After that right click on the References Folder on your WiX setup project and choose Add Reference... .
Click the tab Projects and choose your custom action Project.
For the last step you need to add this code to your Product.wxs:
<Binary Id="CustomActionBinary" SourceFile="$(var.CUSTOMACTIONSNAME.TargetDir)$(var.CUSTOMACTIONSNAME.TargetName).CA.dll" />
<CustomAction Id="CUSTOMACTIONAME" Impersonate="no" BinaryKey="CustomActionBinary" DllEntry="CUSTOMACTIONFUNCTION" Return="check" />
这里只需要改几个名字:
You only need to change a few names here:
- CUSTOMACTIONSNAME = 添加到引用文件夹中的自定义操作的名称(默认为CustomActions")
- CUSTOMACTIONNAME = 为您的自定义操作选择一个名称,例如CreateConfig".
- CUSTOMACTIONFUNCTION = 您要调用的自定义操作项目中的函数名称.
就是这样.
如果您现在想在您的设置项目中调用自定义操作,您只需要创建一个带有 Action 属性的自定义"元素,并将您的自定义操作 id 作为值,如下所示:
If you now want to call the custom action in your setup project, you only need to create a "Custom" element with an Action attribute with your custom action id as value like this:
<Custom Action="CreateConfig" ... />
您可以将自定义操作插入到 UI 序列或安装序列中,如下所示:
You can insert the custom action into the UI sequence or the install sequence as follows:
<!--User Interface Sequence-->
<InstallUISequence>
<Custom Action='CustomAction1' Before='ExecuteAction' />
</InstallUISequence>
<!--Installation Sequence-->
<InstallExecuteSequence>
<Custom Action='CustomAction1' After='InstallInitialize'>NOT Installed</Custom>
</InstallExecuteSequence>
您还可以从对话框中的事件调用自定义操作(仅片段 - 有点涉及):
You can also call a custom action from an event in a dialog box (snippet only - somewhat involved):
<...>
<Control Id="Next" Type="PushButton" X="236" Y="243" Width="56" Height="17" Default="yes" Text="&Next">
<Publish Event="DoAction" Value="CustomAction1">1</Publish>
<...>
这篇关于如何在 c# 中创建自定义操作并将其绑定到 wix 设置项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!