自定义对话框
开始之前,我们先看看标准的WixUI_Mondo UI包含哪些对话框:
1: BrowseDlg
2: CustomizeDlg
3: DiskCostDlg
4: LicenseAgreementDlg
5: SetupTypeDlg
6: WelcomeDlg
这些标准对话框都有Back和Next按钮,他们通过类似双向链表的方式连接起来,新添加的对话框需要插入到这个双向链表中;这里我们需要添加DbConfigDlg对话框,就需要重新定义链表的Back和Next节点。
首先我们创建一个DbConfigDlg.wxs文件,文件内容如下:
<?xml version="1.0" encoding="utf-8"?> <Wix xmlns="http://schemas.microsoft.com/wix/2006/wi"> <Fragment> <UI> <Dialog Id="DBConfigDlg" Width="370" Height="270" Title="[ProductName] [Setup]" NoMinimize="yes"> <Control Id="DBNameLabel" Type="Text" X="45" Y="56" Width="200" Height="15" TabSkip="no" Text="数据库名称:" /> <Control Id="DBNameEdit" Type="Edit" X="45" Y="71" Width="280" Height="15" Property="DBNAME" Text="{32}" /> <Control Id="ServerLabel" Type="Text" X="45" Y="94" Width="200" Height="15" TabSkip="no" Text="服务器(名称或者IP地址):" /> <Control Id="ServerEdit" Type="Edit" X="45" Y="109" Width="280" Height="15" Property="SERVERNAME" Text="{32}" /> <Control Id="UserNameLable" Type="Text" X="45" Y="131" Width="280" Height="10" TabSkip="no"> <Text>用户名:</Text> </Control> <Control Id="UserNameEdit" Type="Edit" X="45" Y="146" Width="280" Height="15" Property="DBUSERNAME" Text="{32}" /> <Control Id="PasswordLabel" Type="Text" X="45" Y="169" Width="200" Height="15" TabSkip="no" Text="密码:" /> <Control Id="PasswordEdit" Type="Edit" Password="yes" X="45" Y="184" Width="280" Height="15" Property="DBPASSWORD" Text="{32}" /> <Control Id="TestConnect" Type="PushButton" X="45" Y="206" Width="66" Height="17" Text="测试连接(&T)"> <Publish Event="DoAction" Value="ConnectDB" Order="2" >1</Publish> <Publish Event="SpawnDialog" Value="InvalidDBDlg" Order="3">1</Publish> </Control> <Control Id="Back" Type="PushButton" X="180" Y="243" Width="56" Height="17" Text="!(loc.WixUIBack)"> <Publish Event="NewDialog" Value="LicenseAgreementDlg">1</Publish> </Control> <Control Id="Next" Type="PushButton" X="236" Y="243" Width="56" Height="17" Default="yes" Text="!(loc.WixUINext)"> <Condition Action="disable"><![CDATA[CONNECTSUCCESS <> "1"]]></Condition> <Condition Action="enable">CONNECTSUCCESS = "1"</Condition> <Publish Event="NewDialog" Value="SetupTypeDlg">1</Publish> </Control> <Control Id="Cancel" Type="PushButton" X="304" Y="243" Width="56" Height="17" Cancel="yes" Text="!(loc.WixUICancel)"> <Publish Event="SpawnDialog" Value="CancelDlg">1</Publish> </Control> <Control Id="BannerBitmap" Type="Bitmap" X="0" Y="0" Width="370" Height="44" TabSkip="no" Text="WixUI_Bmp_Banner" /> <Control Id="Description" NoWrap="no" Type="Text" X="25" Y="23" Width="280" Height="30" Transparent="yes" NoPrefix="yes"> <Text>继续安装应用程序需要填写以下信息,确保数据库能够被正确安装,并且使应用程序能够在该数据库上运行。</Text> </Control> <Control Id="BottomLine" Type="Line" X="0" Y="234" Width="370" Height="0" /> <Control Id="Title" Type="Text" X="15" Y="6" Width="200" Height="15" Transparent="yes" NoPrefix="yes"> <Text>{/WixUI_Font_Title}数据库配置</Text> </Control> <Control Id="BannerLine" Type="Line" X="0" Y="44" Width="370" Height="0" /> </Dialog> <Property Id="DBNAME" Value="test" /> <Property Id="SERVERNAME" Value="(local)" /> <Property Id="DBUSERNAME" Value="sa" /> <Property Id="DBPASSWORD" Value="123456" /> </UI> </Fragment> <Fragment> <UI> <Dialog Id="InvalidDBDlg" Width="250" Height="85" Title="[ProductName] [Setup]" NoMinimize="yes"> <Control Id="Return" Type="PushButton" X="100" Y="57" Width="56" Height="17" Default="yes" Cancel="yes" Text="返回(&R)"> <Publish Event="EndDialog" Value="Return">1</Publish> </Control> <Control Id="ConnError" Type="Text" NoWrap="no" X="48" Y="15" Width="194" Height="30" TabSkip="no"> <Text>数据库连接失败。</Text> <Condition Action="show"><![CDATA[CONNECTSUCCESS <> "1"]]></Condition> <Condition Action="hide">CONNECTSUCCESS = "1"</Condition> </Control> <Control Id="ConnYes" Type="Text" NoWrap="no" X="48" Y="15" Width="194" Height="30" TabSkip="no"> <Text>数据库连接成功。</Text> <Condition Action="show">CONNECTSUCCESS = "1"</Condition> <Condition Action="hide"><![CDATA[CONNECTSUCCESS <> "1"]]></Condition> </Control> </Dialog> </UI> </Fragment> </Wix>
代码中一共有2个对话框,主要由Control组成,由Type属性区分Control类型。InvalidDBDlg对话框用来弹出连接提示信息;DbConfigDlg对话框接受用户录入的数据,包括文本框、输入框和按钮等;我们可以看到每个PushButton类型的Control都包含一个Publish标记,它定义了Event事件属性,Publish的Event属性值包含如下可能:
1、DoAction ,表示按钮事件执行一个Action动作,value属性值是Action的标识
2、NewDialog, 表示按钮事件将当前操作界面定位到新的界面,value属性值是界面的标识
3、SpawnDialog,表示按钮事件将弹出一个模式对话框,value属性值是界面的标识
4、如果Publish标记没有定义Event属性,则必须定义Property属性,但不可同时定义;定义Property属性表示按钮事件会给Property赋值,value属性值是给Property设置的值。
我们来看看这些按钮的作用:
TestConnect 按钮包含两个Publish事件,第一个事件是执行ConnectDB的Action,这个Action也就是我们上节定义的Action;它的第二个事件是弹出InvalidDBDlg对话框,提示连接数据库的提示信息。
Back、Next、Cancel是安装界面的标准按钮,Back按钮的Publish事件将界面跳转到LicenseAgreementDlg欢迎界面;Next按钮的Publish事件将界面跳转到SetupTypeDlg 安装类型选择界面,Next按钮包含两个Condition条件,第一个Condition表示当CONNECTSUCCESS <> "1"时,Next按钮将被禁用,第二个Condition表示当CONNECTSUCCESS <> "0"时,Next按钮将被激活;Cancel的Publish事件将弹出CancelDlg对话框,CancelDlg对话框是windows installer内置的,用户可以选择是否终止或退出安装。
Text和Edit类型的Control相当于我们熟悉的Lable和TextBox。不同的是,Edit编辑控件必须定义Property属性,这样我们在安装过程或者Action中都可以引用该Property值;需要注意的是,这里Property的值才能正确反映编辑框的值,而“Text="{32}”仅表示允许输入的文本框长度,如果想给Control赋初始值,通过Text设置是不起作用的,需要显示定义Property,如:
<Property Id="DBNAME" Value="master" />
<Product ...> <Package .../> ... <Feature...> ... ... </Feature> <Binary Id='ConnectDBClass' SourceFile='$(var.Version)/SampleCustomAction.CA.dll' /> <CustomAction Id='LaunchFile' FileKey='filFoobarEXE' ExeCommand='' Return='asyncNoWait' /> <CustomAction Id='ConnectDB' BinaryKey='ConnectDBClass' DllEntry='ConnectDataBase' /> <UI Id="MyWixUI_Mondo"> <UIRef Id="WixUI_Mondo" /> <UIRef Id="WixUI_ErrorProgressText" /> <DialogRef Id="DBConfigDlg" /> <Publish Dialog="DBConfigDlg" Control="TestConnect" Property="CONNECTIONSTRING" Value="data source=[SERVERNAME];user=[DBUSERNAME];password=[DBPASSWORD];initial catalog=[DBNAME];Persist Security Info=;" Order="1" >NOT Installed</Publish> <Publish Dialog="LicenseAgreementDlg" Control="Next" Event="NewDialog" Value="DBConfigDlg" Order="2"> LicenseAccepted = "1" </Publish> <Publish Dialog="SetupTypeDlg" Control="Back" Event="NewDialog" Value="DBConfigDlg">1</Publish> <Publish Dialog="ExitDialog" Control="Finish" Event="DoAction" Value="LaunchFile" Order="1">1</Publish> </UI> <Property Id="WIXUI_INSTALLDIR" Value="INSTALLDIR" /> </Product>
candle.exe -dVersion=1.0.0 1.0.0/Sample.wxs DbConfigDlg.wxs -out 1.0.0/ light.exe -loc WixUI_zh-cn.wxl -ext WixUIExtension -out 1.0.0/Sample.msi 1.0.0/Sample.wixobj 1.0.0/DbConfigDlg.wixobj