如何在C#中创建自定义操作并将其绑定到Wix安装项目上 [英] How to create custom actions in c# and bind it on a wix setup project
问题描述
如何创建自定义操作并将其链接到我的WiX设置项目?
我有:
How do I create custom actions and link it to my WiX setup project?
I have:
- WiX 3.11
- Visual Studio
推荐答案
您需要在解决方案中创建一个新的用于WiX v3的C#自定义操作项目.
看起来应该像这样:
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设置项目中的 参考文件夹 ,然后选择添加参考... .
单击选项卡项目,然后选择您的自定义操作项目.
对于最后一步,您需要将此代码添加到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属性的"Custom"元素,并将您的自定义操作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安装项目上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!