Using IWizard in an item template without installi

2019-01-22 21:54发布

问题:

I would like to create a custom item template that includes a wizard (IWizard interface).

On http://msdn.microsoft.com/en-us/library/vstudio/ms171411%28v=vs.100%29.aspx and some other places it is always described that the assembly containing the wizard has to be installed in GAC. I would like to distribute my template in my organisation, and not everybody has right to install anything into GAC.

So I'm wondering, is there no way to have the assembly containing the wizard code inside the template zip, or vsix file?

It seems to me that this should be a common problem?

Any help how to distribute a custom wizard and item template without any GAC installations?

回答1:

After a lot of research, I found a way to do that. With this method it is possible to include the assembly with the IWizard implementation into the VSIX file, even without signing it, and having it available at runtime. No need to fiddle with the GAC.

You need 3 projects in your solution

  1. the vsix project (we call it MyExtension)
  2. an item template project (we call it MyTemplate)
  3. a regular class library project for the wizard code (we call it MyWizardImpl)

Inside MyWizardImpl you need to implement the IWizard interface, in an arbitray class. (we call it MyWizardImpl.ItemTemplateWizard1). When compiling this, you get as output

<solutiondir>\MyWizardImpl\bin\Debug\MyWizardImpl.dll

Now, in MyTemplate\MyTemplate.vstemplate, you need to reference it

<WizardExtension>
    <Assembly>MyWizardImpl</Assembly>
    <FullClassName>MyWizardImpl.ItemTemplateWizard1</FullClassName>
</WizardExtension>

The tricky part comes last: Including the assembly inside the vsix in a way that it can be loaded when the ItemTemplate is applied.

  • Create a folder MyExtension\Assemblies
  • In solution explorer, Right-click that folder and choose Add -> Existing Item...
  • In the "Add existing Item" dialog, navigate to the created MyWizardImpl.dll. On the Add button, click on the little arrow and choose "Add As Link" (to prevent that VS makes a copy of the dll)
  • In the properties for this link, choose BuildAction=Content, IncludeInVSIX=True
  • Open the vsixmanifest in a XML editor (or notepad), and add an Assembly line to the section

Example:

<Content>
   <Assembly AssemblyName="MyWizardImpl">Assemblies\MyWizardImpl.dll</Assembly>
</Content>

Build and test the solution, the wizard code should run now when using the template.

If you need more than one template in your vsix, you simply need to add additional ItemTemplate projects. All the wizards can go into the same MyWizardImpl assembly.