当使用Custom Action 时,时常会需要按照一定的顺序去执行相应的Custom Action,这时需要使Execute="deferred" 然而在执行相应的脚本时,去获取相应的session时却会为空或不存在。
例如:
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2003/01/wi">
<Product Id="{94A35E02-D48F-48F1-AC1A-23F62489BBF4}"
Name="Minimal Windows Installer Sample"
Language="1033"
Codepage="1252"
Version="1.0.0"
Manufacturer="Acme Corporation"
UpgradeCode="{74069BA4-1D1C-4252-A074-B2EC0C746403}">
<Package Id="{4C159FD5-135E-42DE-B36E-22DDDCBA3DCF}"
Description="Minimal Windows Installer Sample"
Comments="This installer database contains the logic and data required to install [ProductName]."
InstallerVersion="200"
Languages="1033"
SummaryCodepage="1252"
Platforms="Intel"
ReadOnly="no"
Compressed="yes"
AdminImage="no"
Keywords="Installer"
ShortNames ="no"
Manufacturer="Acme Corporation" />
<Media Id="1" Cabinet="CAB001.cab" EmbedCab="yes" />
<Directory Id="TARGETDIR" Name="SourceDir">
<Directory Id="ProgramFilesFolder">
<Directory Id="INSTALLDIR" Name="Minimal" LongName="MinimalInstallation">
<Component Id="Component1"
Guid="{A77C5B06-132D-4884-8E17-EA10A83C812D}">
<File Id="ReadMe" DiskId="1" Name="Readme.txt" Source="Readme.txt" Vital="yes" KeyPath="yes" />
</Component>
</Directory>
</Directory>
</Directory>
<Property Id="TESTPROPERTY" Value="Hello" />
<CustomAction Id="ShowProperty" Script="vbscript" Execute="deferred">
<![CDATA[
MsgBox Session.Property("TESTPROPERTY")
]]>
</CustomAction>
<InstallExecuteSequence>
<Custom Action="ShowProperty" Before="InstallFinalize">Not Installed</Custom>
</InstallExecuteSequence>
<Feature Id="Feature1"
Title="Feature1 title"
Description="Feature1 description"
Level="1"
ConfigurableDirectory="INSTALLDIR" >
<ComponentRef Id="Component1" />
</Feature>
</Product>
</Wix>
执行后会发现弹出的对话框为空。而如果使用Execute="immediate"(缺省设置)时会发现会弹出相应的目录。
这是因为延迟方式的Custom Action 在获取相应的session时,session已经销毁了,
则延迟方式的Custom Action 只能使用以下三种property:
ProductCode
UserSID
CustomActionData
使用customActionData方式(即先将需要的property保存):
<Property Id="TESTPROPERTY" Value="Hello" />
<CustomAction Id="ShowProperty" Script="vbscript" Execute="deferred">
<![CDATA[
MsgBox Session.Property("CustomActionData")
]]>
</CustomAction>
<CustomAction Id="SetPropertyForShowProperty"
Property="ShowProperty" Value="[TESTPROPERTY]" />
<InstallExecuteSequence>
<Custom Action="SetPropertyForShowProperty" Before="InstallInitialize">Not Installed</Custom>
<Custom Action="ShowProperty" Before="InstallFinalize">Not Installed</Custom>
</InstallExecuteSequence>
如需要多个property:
<Property Id="TESTPROPERTY" Value="Hello" />
<Property Id="TESTPROPERTY2" Value="World" />
<CustomAction Id="ShowProperty" Script="vbscript" Execute="deferred">
<![CDATA[
Dim properties
properties = Split(Session.Property("CustomActionData"), ";", -1, 1)
MsgBox properties(0) & ", " & properties(1) & "!"
]]>
</CustomAction>
<CustomAction Id="SetPropertyForShowProperty"
Property="ShowProperty"
Value="[TESTPROPERTY];[TESTPROPERTY2]" />
<InstallExecuteSequence>
<Custom Action="SetPropertyForShowProperty" Before="InstallInitialize">Not Installed</Custom>
<Custom Action="ShowProperty" Before="InstallFinalize">Not Installed</Custom>
</InstallExecuteSequence>