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

问题描述

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

<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;
        }

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

预先感谢

解决方法

这是一个工作中的自定义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_Title}Database 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_Normal}Before 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_Normal}Server:" />
        <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_Normal}Port:" />
        <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_Normal}Database:" />
        <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_Normal}User:" />
        <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_Normal}Password:" />
        <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;
            }
        }
    }
}