首页 > 解决方案 > 验证 WIX 安装程序中的输入控件

问题描述

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

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

提前致谢

标签: wixwix3.7

解决方案


这是一个工作自定义 Wix 对话框

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

数据库连接信息.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>

自定义动作.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;
            }
        }
    }
}

推荐阅读