Go deep into the IWizard interface (I)

Source: Internet
Author: User

In this article, we will study the IWizard interface.

1. A trace interface implements LifeCycleTracerIWizard.

The IWizard interface is used by Visual Studio to manage instantiation and execution methods. That is to say, the life cycle of its instance type is not controlled by us, so we can discuss such a life cycle with another idea, so I define such an IWizard to implement LifeCycleTracerIWizard, which is used to track the execution sequence of each method in the interface and context information, that is, the parameter value.

The LifeCycleTracerIWizard. cs code is as follows:

   1: public class LifeCycleTracerIWizard : IWizard
   2: {
   3:     private string loggerFilePath = @"D:\LifeCycleTracerWizard.log";
   4:     private string safeProjectName = "[None]";
   5:  
   6:     public virtual String WizardType
   7:     {
   8:         get { return "LifeCycleTracerIWizard"; }
   9:     }
  10:  
  11:     private void LogWizardMethodAction(string methodAction)
  12:     {
  13:         using (StreamWriter streamWriter = new StreamWriter(loggerFilePath,true))
  14:         {
  15:             streamWriter.WriteLine(WizardType.PadRight(30) + "-" + safeProjectName.PadRight(15) + ":" + methodAction);
  16:         }
  17:     }
  18:  
  19:     #region Implementation of IWizard
  20:  
  21:     public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
  22:     {
  23:         if(replacementsDictionary != null && replacementsDictionary.ContainsKey("$safeprojectname$"))
  24:         {
  25:             safeProjectName = replacementsDictionary["$safeprojectname$"];
  26:         }
  27:  
  28:         LogWizardMethodAction("RunStarted : " + runKind);
  29:     }
  30:  
  31:     public void ProjectFinishedGenerating(Project project)
  32:     {
  33:         LogWizardMethodAction("ProjectFinishedGenerating");
  34:     }
  35:  
  36:     public void ProjectItemFinishedGenerating(ProjectItem projectItem)
  37:     {
  38:         LogWizardMethodAction("ProjectItemFinishedGenerating");
  39:     }
  40:  
  41:     public bool ShouldAddProjectItem(string filePath)
  42:     {
  43:         LogWizardMethodAction("ShouldAddProjectItem : " + filePath);
  44:         return true;
  45:     }
  46:  
  47:     public void BeforeOpeningFile(ProjectItem projectItem)
  48:     {
  49:         LogWizardMethodAction("BeforeOpeningFile");
  50:     }
  51:  
  52:     public void RunFinished()
  53:     {
  54:         LogWizardMethodAction("RunFinished");
  55:     }
  56:  
  57:     #endregion
  58: }
2. Use LifeCycleTracerIWizard to track sub-projects of multiple project templates.

In the previous article, we modified the VSTemplate-WizardExtension-FullClassName subnode value to: Ethan. woo. templateWizard. lifeCycleTracerIWizard: Create an instance after deployment and view the generated Log file "D: \ LifeCycleTracerWizard. log content:

The results show the following conclusions:

1) The Ethan. Woo. TemplateWizard. LifeCycleTracerIWizard interface specified in the template metadata file is instantiated.

2) The execution sequence of each method is RunStarted> ShouldAddProjectItem> ProjectFinishedGenerating> RunFinished. Note that two methods are not executed, and ProjectItemFinishedGenerating is used for the project template wizard, so it will not be executed here; BeforeOpeningFile is not executed either, because it will be used when we make Starter Kit, so we will discuss this topic again later.

3) The third parameter of RunStarted is a WizardRunKind enumeration (which has three values: AsMultiProject, AsNewItem, and AsNewProject). The value tracked here is AsNewProject, which indicates creating a new project.

4) How many times has the ShouldAddProjectItem method been executed? Yes, the project in our project source contains multiple sub-files, so it is obviously called when adding each sub-file in the template instantiation stage, and it has a Boolean return value, to add the current sub-file to the project instance.

3. Use multiple IWizard interfaces to track sub-projects of multiple project templates.

First, extract the original text from MSDN:

Visual Studio supports chaining, which enables a single template to have multiple IWizard implementations. These implementations are called sequentially, so you can create templates that have rich, flexible functionality.

A Microsoft-implemented VsTemplate wizard is invoked to process a template by reading its. vstemplate file. A VsTemplate can list one or more assemblies that have an IWizard implementation that will be called to particle in processing the template. to take advantage of chaining, all the wizards must be listed in the template's. xml file in the order in which they shocould be called.

Visual Studio supports the series of IWizard interfaces implemented by a single template and is executed sequentially. It can also be used to deduce VSTemplate. This node can contain multiple WizardExtension settings, implement such a series.

Here we define a new implementation of the ChainingLifeCycleTracerIWizard interface, inheriting the previous LifeCycleTracerIWizard;

   1: public class ChainingLifeCycleTracerIWizard : LifeCycleTracerIWizard
   2: {
   3:     public override string WizardType
   4:     {
   5:         get
   6:         {
   7:             return "ChainingLifeCycleTracerIWizard";
   8:         }
   9:     }
  10: }

 

Then, we concatenate the ChainingLifeCycleTracerIWizard and the previous LifeCycleTracerIWizard to the template of the WebClient project. Of course, we still use the template metadata file. Pay attention to the Declaration Order here;

   1: <VSTemplate xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Version="3.0.0" Type="Project">
2 :... This is omitted...
   3:   <WizardExtension>
   4:     <Assembly>Ethan.Woo.TemplateWizard, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e82b5e824e88ddd5</Assembly>
   5:     <FullClassName>Ethan.Woo.TemplateWizard.LifeCycleTracerIWizard</FullClassName>
   6:   </WizardExtension>
   7:   <WizardExtension>
   8:     <Assembly>Ethan.Woo.TemplateWizard, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e82b5e824e88ddd5</Assembly>
   9:     <FullClassName>Ethan.Woo.TemplateWizard.ChainingLifeCycleTracerIWizard</FullClassName>
  10:   </WizardExtension>
  11: </VSTemplate>

 

Finally, let's take a look at the Log file "D: \ LifeCycleTracerWizard. log:

Isn't it interesting? I 've done it all over again. Don't explain it.

 

From: http://www.ethan-woo.com/post/2011/05/03/Deep-into-IWizard-First.aspx

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.