Wizard Framework: A self-developed Windows Forms-based Wizards development framework

Source: Internet
Author: User
Tags git client

Recently, due to project needs, I have designed and developed a Windows Forms-based wizard Development framework that I have now open up and released a NuGet installation package. One of the more embarrassing things is that when I released the NuGet installation package, I found that there was already a. NET, which is called the Microsoft Visual Studio Wizard framework. I did not delve into it, just from the name, is it only available under Visual Studio 2013? Internet search, also did not find Microsoft has more detailed official information about the framework. However, I am still here to introduce myself to the framework of the wizard, but also to let you know my design ideas, and to make it easy to benefit from the framework, and quickly in their own projects to use the wizard framework.

There's a picture of the truth

Speak not much, please look first. To demonstrate this framework, I relied on it to develop a wizard that simulates the software installation process. Users who have used installation programs like Install shield should be familiar with the following dialog boxes:

What do you think? Looks like a professional, huh? It was developed using the wizard framework. The 1.0.0 version supports the following features:

    • Wizard dialogs can be customized, such as the size of the dialog box, Icon, whether to support online help, etc.
    • The wizard page design supported by the Windows Forms Designer allows developers to design the interface of each page directly in Visual Studio, just as with a user control, by using a drag-and-drop approach.
    • Each page can directly set the status of the previous, next, complete, and Cancel buttons in the wizard dialog box via Cangopreviouspage, Cangonextpage, Cangofinishpage, and Cangocancel four properties
    • Each page can read the wizard model (Wizardmodel) saved by any other page, get the setting parameters of each page through the wizard model (for example, the data from the "Software Feature Selection" page is read and displayed in the "Installation Information Summary" page above)
    • Each page can be directly set whether the other page is visible in the previous step or next, for example, in some cases, when a parameter of the current page is set, we want to be able to skip the next page when the point "next", and go directly to the next page
    • Each page can set its own logo
    • Support for async/await in C # 5.0 makes the wizard-oriented asynchronous development model extremely simple
    • Support Chinese and English

So, how do I get the source code or the development package?

Source code and NuGet installation package

You can directly access the wizard Framework's home page: Https://github.com/daxnet/wizard-framework. If you have a git client, you can clone the project locally:

git clone https://github.com/daxnet/wizard-framework

When cloning is complete, open WizardFramework.sln directly in Visual Studio 2013 (Currently the wizard Framework is based on the. NET Framework 4.5.1, so it is recommended to use Visual Studio 2013 open). At this point, you can see that the solution consists of two items:

The Wizardframework project is the source code for the framework, and Installersample is a Windows Forms application that uses Wizardframework to develop a wizard interface for the simulation software installation process. That's what you see on the interface. You can modify the first statement of the main function in Program.cs, set the localization information to ZH-CN or en-us to experience the interface effect of the simulator in different regional languages.

If you want to use the wizard Framework in your own Windows Forms project, you can right-click on the project, select the Manage NuGet Packages menu item, Search the Wizardframework keyword in the popup dialog box:

When selected, click the Install button to add the wizard development Framework to your project (note: the recommended application was developed based on the. NET Framework 4.5.1).

How to use

After you add a reference to the wizard framework through the NuGet Package Manager, you are ready to start the Development wizard application. There are basically three steps: the Development wizard page, The Development wizard dialog box, and the wizard page that you added to the wizard dialog box.

Development Wizard Page

To develop a wizard page, simply add a user control (UserControl) on the Windows Forms project of Visual Studio and then inherit it from the Wizardframework.wizardpage class. At this point, the Visual Studio editor prompts for a constructor error because the Wizardpage type has no accessible default constructor, which requires passing parameters to the base class through the constructor of the custom wizard page class. The following is an overload of the Wizardpage constructor and an argument description for each overloaded constructor.

  • Wizardpage (string title, String Description, Wizard Wizard, Iwizardmodel model = null)
    • Title: The header information used to display the bold title section above each wizard page
    • Description: Used to display the wizard page description information on top of each wizard page
    • Wizards: The wizard objects that are currently in the wizard page, typically passed in through the constructor parameters of the wizard page
    • Model: Data object models used by the current wizard page
  • Wizardpage (string title, String Description, Wizard Wizard, Wizardpagetype type)
    • title: Used to display header information in the bold title section above each wizard page
    • Description: Used to display the wizard page description information at the top of each wizard page
    • Wizard: The current wizard page, which is typically passed through the constructor parameter of the wizard page to the
    • type: The type of the current wizard page. It is divided into two types: standard and expanded. The standard type means that when the wizard page is displayed, the title and description information is displayed at the top of the wizard dialog box, and for the expanded type, the information is not displayed, and the entire page is designed entirely by the developer. Obviously, in the example above, 2, 3, 4, 5 pages are wizard pages of the standard type, while 1 and 6 pages belong to the expanded type
  • Wizardpage (string title, String Description, Wizard Wizard, Iwizardmodel model, Wizardpagetype type)
    • Title: The header information used to display the bold title section above each wizard page
    • Description: Used to display the wizard page description information on top of each wizard page
    • Wizards: The wizard objects that are currently in the wizard page, typically passed in through the constructor parameters of the wizard page
    • Model: Data object models used by the current wizard page
    • Type: The types of the current wizard pages. It is divided into two types: standard and expanded. The standard type means that when the wizard page is displayed, the title and description information is displayed at the top of the wizard dialog box, and for the expanded type, the information is not displayed, and the entire page is designed entirely by the developer. Obviously, in the example above, 2, 3, 4, 5 pages are wizard pages of the standard type, while 1 and 6 pages belong to the expanded type

The following is a class definition for a wizard page and an example implementation of a constructor:

Public partial class firstpage:wizardpage{public    FirstPage (Wizard Wizard)        : Base ("first page", "This is the Firs T page. ", Wizard)    {        InitializeComponent ();    }}

It is important to note that the constructor of the wizard page must be public and has only one parameter of type Wizard.

Several callback functions in the wizard page

In the Wizardframework.wizardpage base class, you define a number of callback functions that can be called by the wizard object, which are called at the appropriate time, so that developers can handle their own logic in these callback functions, such as setting whether to allow users to click on "Next page "and other navigation buttons.

  • "Method"Task executeshowasync (iwizardpage frompage)
    • This method is called when the wizard prepares to display the current page. The method calls asynchronously
    • FromPage parameter: Indicates which wizard page was navigated from. For example, when a user taps the next page button, the next wizard page appears in the wizard dialog box, typically the FromPage parameter is the previous page of the wizard page that is displayed
  • "Method"task<bool> executebeforegoingpreviousasync ()
    • This method is called when the wizard dialog box is ready to go to the previous wizard page when the user taps the previous page button. The method calls asynchronously
    • Return value: Returns a Task object that returns a Boolean value that indicates whether the wizard dialog box is really allowed to go to the previous page (true= allowed; false= not allowed)
  • "Method"task<bool> executebeforegoingnextasync ()
    • This method is called when the wizard dialog box is ready to go to the next wizard page when the user taps the next page button. The method calls asynchronously
    • Return value: Returns a Task object that returns a Boolean value that indicates whether the wizard dialog box is really allowed to go to the next page (true= allowed; false= not allowed)
  • "Method"task<bool> executebeforegoingfinishasync ()
    • This method is called when the wizard dialog box is ready to go to the Finish wizard page when the user taps the Finish button. The method calls asynchronously
    • Return value: Returns a Task object that returns a Boolean value that indicates whether the wizard dialog box is really allowed to end and returns the DialogResult.OK value to the caller (true= allowed; false= not allowed)
  • "Method"void Persistvaluestomodel ()
    • This method is called when the wizard dialog box is ready to go to another wizard page, and the user settings on the currently Displayed wizard page interface are saved to the wizard data model object, which is described in detail in the next section
  • "Properties"System.Windows.Forms.Control Focusingcontrol
    • Sets the interface control on which the focus (focus) is located when the current wizard page is opened (that is, which control the focal point should be on)
  • "Properties"System.Drawing.Image Logo
    • Set the icon that the current wizard page needs to appear in the upper-right corner of the wizard dialog box

When a developer customizes its own wizard page, the above methods or properties can be overloaded in subclasses to handle different logic at different times. For detailed usage, you can refer to the Installersample sample project that comes with the Wizardframework source code library.

Wizard Data Model

In Wizardframework, you save the user settings for each wizard page by introducing the concept of a wizard data model. For example, on the Featurepage wizard page of the Installersample sample project, users can select the type of installation (minimum, Standard, and full) on the interface, and you can specify the installation path. These user settings are saved in the data Model of the wizard page so that other wizard pages or wizard dialogs can be read and used. The definition of the wizard data Model class requires implementing the Iwizardmodel interface as follows:

Public sealed new class model:iwizardmodel{    #region public Properties public    string Selectedfeature {get; set; Public    string Selectedfolder {get; set;}    #endregion public Properties    #region public Methods public    override string ToString ()    {        var sb = new Stri Ngbuilder ();        Sb. Appendline (String. Format (Resources.selectedfeaturepattern, selectedfeature));        Sb. Appendline (String. Format (Resources.installationfolderpattern, Selectedfolder));        Return SB. ToString ();    }    #endregion Public Methods}

In the data model object, you only need to write some properties that correspond to the value of the interface control. A recommended practice is to define the wizard data model class in the class definition of each wizard page, which is defined as an inline class of the wizard page class, simply using model as the class name, in order to bypass the compiler warning, add the new keyword when declaring the class. The benefit of this is that you can help improve the readability of your code when you get the wizard Model objects in other wizard pages or in the wizard dialog box in the future.

If the wizard page needs to use the wizard data model, you need to initialize the data model object in the constructor, as follows (note the last parameter of the base constructor call):

Public Featurepage (Wizard Wizard)    : Base (Resources.featurepagetitle, Resources.featurepagedescription, Wizard, New Model ()) {    InitializeComponent ();}

Also, you need to overload the Persistvaluestomodel method to save the interface control's value in the data model:

protected override void Persistvaluestomodel () {    var selectedfeature = string. Empty;    if (rbminimal.checked)        selectedfeature = Rbminimal.text;    else if (rbstandard.checked)        selectedfeature = Rbstandard.text;    else if (rbfull.checked)        selectedfeature = Rbfull.text;    Modelas<model> (). Selectedfeature = selectedfeature;    Modelas<model> (). Selectedfolder = Txtinstpath.text;}

You can use the following methods when you need to get a data model object for a wizard page in another page, or through a wizard dialog box:

var model = wizard.getwizardmodel<featurepage.model> ();

Here, the Featurepage.model data model object is obtained through the Getwizardmodel generic method of the wizard object.

Navigation of the Control wizard

In some scenarios, you need to decide which wizard page the next page should navigate to, based on some of the interface settings for the current page. For example, on page 1 of the wizard, if the user clicked on a check box, then when the user points to the Next button, skip page 2 and go directly to Page 3, otherwise you will need to skip to page 2. At this point, you can invoke the Setpagedisplay method of the wizard object. There are two overloads of this method:

    • void Setpagedisplay (int pageIndex, wizardpagedisplay display)
      • PageIndex: Based on the Order of the wizard pages added to the wizard dialog box, the corresponding wizard page index number
      • Display: Specifies whether the page is displayed (wizardpagedisplay.show) or not displayed (Wizardpagedisplay.hide)
    • void Setpagedisplay<t> (Wizardpagedisplay display)
      • Generic type T: Specifies the type of wizard page that you want to set the display behavior for
      • Display: Specifies whether the page is displayed (wizardpagedisplay.show) or not displayed (Wizardpagedisplay.hide)
Development Wizard dialog box

The Development of the wizard dialog box is simple, just create a new System.Windows.Forms.Form type, and then inherit it from the Wizardframework.wizard class, without having to write any more code. Of course, if you need to set some additional properties, you can also set them directly in Visual Studio's property pages.

Initialize the wizard page and Add the wizard page to the wizard dialog box

The following code shows how the wizard page is initialized and added to the wizard dialog box, or very simple:

var installer = new Frminstaller (); installer. ADD (installer. Createpage<welcomepage> ()); installer. ADD (installer. Createpage<licensepage> ()); installer. ADD (installer. Createpage<featurepage> ()); installer. ADD (installer. Createpage<summarypage> ()); installer. ADD (installer. Createpage<installingpage> ()); installer. ADD (installer. Createpage<finishpage> ());
Summarize

This article describes a wizard framework that I developed myself and describes the use of the framework. Perhaps, in some cases, the framework is still unable to meet the requirements, at this time, you can directly pull down the source code wizardframework to customize.

Wizard Framework: A self-developed Windows Forms-based Wizards development framework

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.