验证 WIX 安装程序中的输入控件

Posted

技术标签:

【中文标题】验证 WIX 安装程序中的输入控件【英文标题】:To validate the input controls in WIX Installer 【发布时间】:2020-08-21 07:30:00 【问题描述】:

我是 WIX 的新手。我想验证文本框等输入控件不为空,密码和确认密码是否相同。 我尝试在自定义操作中执行此操作,但无法发送参数。 如果我发送参数如何返回值以留在同一安装页面中。

<Dialog Id="XXX" Width="370" Height="270" Title="Installation">
<Control Id="Next" Type="PushButton" X="236" Y="243" Width="56" Height="17" Default="yes" Text="!(loc.WixUINext)" >
<Publish Event="DoAction" Value="CheckingPID">1</Publish>
<Publish Event="SpawnDialog" Value="InvalidPidDlg">PIDACCEPTED = "0"</Publish>
<Control Id="Usernamelbl" Type="Text" X="20" Y="100" Width="95" Height="10" NoPrefix="yes" Property="WIXUI_INSTALLDIR" Text="Username:" />
<Control Id="UsernameVal" Type="Edit" X="125" Y="100" Width="200" Height="17" Property="SETUSERNAME"  Indirect="no" Disabled="no" />

</Dialog>
<CustomAction Id="CheckingPID"  BinaryKey="CustomActionBinary"   Impersonate="no"  DllEntry="Validate"  Execute="immediate" Return="check"/>
  [CustomAction]
        public static ActionResult Validate(Session session)
        
        MessageBox.Show(Session.CustomActionData["SETUSERNAME"]);
        
            return ActionResult.Success;
        

这是正确的验证方式还是其他任何验证方式。

提前致谢

【问题讨论】:

【参考方案1】:

这是一个有效的自定义 Wix 对话框

此代码使用自定义操作创建自定义 UI 对话框。此对话框的目的是在我们安装桌面应用程序时,我们可以设置 Db 连接信息。

DbConnectionInfo.wxs

<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
  <Fragment>
    <Property Id="Server" Value="127.0.0.1" />
    <Property Id="Port" Value="XXX" />
    <Property Id="Database" Value="DbName" />
    <Property Id="User" Value="root" />
    <Property Id="Password" Value="1234abcA" />
    <UI Id="DbConnectionDlgUI">

      <Dialog Id="DbConnectionDlg" Width="400" Height="275" Title="Demo : Database Connection Settings">

        <Control Id="headerText"  Type="Text" X="140" Y="10" Width="260" Height="40" Transparent="no"
                 Text="\WixUI_Font_TitleDatabase Connection Settings Screen" />

        <Control Id="SideBar" Type="Bitmap" Text="WixUIBannerBmp" X="0" Y="0" Height="240" Width="130" Image="yes" />

        <Control Id="explanationText" X="140" Y="50" NoWrap="no" RightAligned="no" Transparent="yes"
                 Type="Text" Width="260" Height="100"
                 Text="\WixUI_Font_NormalBefore you can use this Service, you need to provide your My Sql Connection settings which is used getting the email database information. If you choose not to install this application, click on the Cancel button to exit." />

        <Control Id="ServerLabel" Type="Text" X="160" Y="120" Height="17" Width="65" Transparent="yes" Text="\WixUI_Font_NormalServer:" />
        <Control Id="ServerTextBox" Type="Edit" X="230" Y="120"   Height="17" Width="60" Property="Server" />

        <Control Id="PortLabel" Type="Text" X="295" Y="120" Height="17" Width="21" Transparent="yes" Text="\WixUI_Font_NormalPort:" />
        <Control Id="PortTextBox" Type="Edit" X="325" Y="120"   Height="17" Width="30" Property="Port" />

        <Control Id="DatabaseLabel" Type="Text" X="160" Y="140" Height="17" Width="65" Transparent="yes" Text="\WixUI_Font_NormalDatabase:" />
        <Control Id="DatabaseTextbox" Type="Edit" X="230" Y="140"  Height="17" Width="120" Property="Database" />

        <Control Id="UserLabel" Type="Text" X="160" Y="160" Height="17" Width="65" Transparent="yes" Text="\WixUI_Font_NormalUser:" />
        <Control Id="UserTextbox" Type="Edit" X="230" Y="160"  Height="17" Width="120" Property="User" />

        <Control Id="PasswordLabel" Type="Text" X="160" Y="180" Height="17" Width="65" Transparent="yes" Text="\WixUI_Font_NormalPassword:" />
        <Control Id="PasswordTextbox" Type="Edit" X="230" Y="180"  Height="17" Width="120" Property="Password" Password="yes"/>

        <Control Id="bottomLine" Type="Line" X="130" Y="239" Width="270" Height="1"/>

        <Control Id="Back" Type="PushButton" Text="Back"  X="208" Y="248" Height="17" Width="60" >
        </Control>
        <Control Id="Next" Type="PushButton" Text="Next"  X="269" Y="248" Height="17" Width="60" Default="yes">
          <Publish Event="DoAction" Value="CreateDbConnectionProperties">1</Publish>
        </Control>
        <Control Id="Cancel" Type="PushButton" Text="Cancel" X="330" Y="248" Height="17" Width="60" Cancel="yes">
          <Publish Event="DoAction" Value="CleanUpAction">1</Publish>
          <Publish Event="NewDialog" Value="CancelDlg" Order="2">1</Publish>
        </Control>

      </Dialog>
    </UI>
  </Fragment>

  <Fragment>    
    <CustomAction Id="CreateDbConnectionProperties" BinaryKey="CustomActionBinary" DllEntry="CreateDbConnectionProperties"  />
  </Fragment>
</Wix>

CustomAction.cs

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Deployment.WindowsInstaller;
using System.IO;

namespace Demo.InstallerActions

    public class CustomlActions
    
        private readonly static string AppName = "Demo";
       
        [CustomAction]
        public static ActionResult CreateDbConnectionProperties(Session session)
        
            session.Log("Saving Db Details Started.");
            try
            
                string Server = session["Server"].Encrypt(AppConstants.SecurityKey,true);
                string Database = session["Database"].Encrypt(AppConstants.SecurityKey, true);
                string User = session["User"].Encrypt(AppConstants.SecurityKey, true);
                string Password = session["Password"].Encrypt(AppConstants.SecurityKey, true);
                string Port = session["Port"].Encrypt(AppConstants.SecurityKey, true);

                string[] confData =  Server, Database, User, Password, Port ;

                string appdataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

                if (!Directory.Exists($"appdataPath\\AppName"))
                
                    Directory.CreateDirectory($"appdataPath\\AppName");
                

                File.WriteAllLines($"appdataPath\\AppName\\conf.sys", confData);

                session.Log("Db Details Saved");

                return ActionResult.Success;
            
            catch (Exception ex)
            
                session.Log($"Configuration File Creation Failed with Error: ex.Message");
                return ActionResult.Failure;
            
        

        [CustomAction]
        public static ActionResult CleanUpAction(Session session)
        
            session.Log("Cleanup Started.");
            try
            
                string appdataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

                if (!Directory.Exists($"appdataPath\\AppName"))
                    Directory.Delete($"appdataPath\\AppName", true);

                session.Log("Db Details Saved");

                return ActionResult.Success;
            
            catch (Exception ex)
            
                session.Log($"Cleanup Error: ex.Message");
                return ActionResult.Failure;
            
        
    

【讨论】:

其实可以的。我可以进行所有验证,但问题是我无法转到下一页。当自定义操作调用时,只有 ActionResult.Failure 有效,我尝试了其他返回枚举,它也没有帮助我.. 建议我验证成功后如何前往下一页。 我添加了更多基于同一个安装程序的代码。请在此处查看。 ***.com/questions/59685153/…

以上是关于验证 WIX 安装程序中的输入控件的主要内容,如果未能解决你的问题,请参考以下文章

在Wix Toolset中的卸载过程中隐藏/显示控件

使用 wix 安装程序,当用户在编辑框中输入内容时安装时我如何在设置过程中覆盖配置文件

Wix XmlConfig 验证路径行为

恢复到以前版本的 wix 安装程序

在自定义对话框中自定义 Wix 安装程序中的图像

Wix - 自定义操作返回代码 - 处理