VS2012模板向导 - GUI未显示

时间:2013-06-15 09:08:20

标签: c# visual-studio visual-studio-2012 .net-assembly visual-studio-templates

我的视觉工作室模板向导的gui出现时遇到了问题。我按照以下步骤操作:http://msdn.microsoft.com/en-us/library/ms185301.aspx

这就是我的所作所为:

1)使用以下文件生成C#类库(.dll):

UserInputForm.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace myprojectvstemplate
{
    public partial class UserInputForm : Form
    {
        private string customMessage;

        public UserInputForm()
        {

            InitializeComponent();
            MessageBox.Show("here, calling ui");
        }

        public string get_CustomMessage()
        {
            return customMessage;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            customMessage = textBox1.Text;

            this.Dispose();
        }
    }
}

2)添加了一个用户输入表单,其中包含一个编辑框和一个代码为UserInputForm.cs的组合框

using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TemplateWizard;
using System.Windows.Forms;
using EnvDTE;

namespace myprojectvstemplate
{
    public class IWizardImplementation : IWizard
    {
        private UserInputForm inputForm;
        private string customMessage;

        // This method is called before opening any item that 
        // has the OpenInEditor attribute.
        public void BeforeOpeningFile(ProjectItem projectItem)
        {

        }

        public void ProjectFinishedGenerating(Project project)
        {

        }

        // This method is only called for item templates,
        // not for project templates.
        public void ProjectItemFinishedGenerating(ProjectItem
            projectItem)
        {

        }

        // This method is called after the project is created.
        public void RunFinished()
        {

        }

        public void RunStarted(object automationObject,
            Dictionary<string, string> replacementsDictionary,
            WizardRunKind runKind, object[] customParams)
        {


            try
            {
                // Display a form to the user. The form collects 
                // input for the custom message.
                inputForm = new UserInputForm();
                inputForm.ShowDialog();

                customMessage = inputForm.get_CustomMessage();

                // Add custom parameters.
                replacementsDictionary.Add("$custommessage$",
                    customMessage);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        // This method is only called for item templates,
        // not for project templates.
        public bool ShouldAddProjectItem(string filePath)
        {

            return true;
        }
    }
}

3)生成公钥/私钥,并在属性页的“签名”选项卡中注册程序集

4)释放生成的dll

5)用gacutil / i mydllname.dll注册,没有错误

6)仅使用一个文件创建了一个C ++控制台项目模板:

#include <iostream>
using namespace std;


int main(int argc, char** argv)
{

    cout << "Hi hello world:" << "$custommessage$";

    return 0;
}

7)导出为模板项目(不是项目),复选框“自动导入vs”。在zip文件中修改.vstemplate文件,如下所示:

<VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005" Type="Project">
  <TemplateData>
    <Name>myproject_project</Name>
    <Description>&lt;No description available&gt;</Description>
    <ProjectType>VC</ProjectType>
    <ProjectSubType>
    </ProjectSubType>
    <SortOrder>1000</SortOrder>
    <CreateNewFolder>true</CreateNewFolder>
    <DefaultName>myproject_project</DefaultName>
    <ProvideDefaultName>true</ProvideDefaultName>
    <LocationField>Enabled</LocationField>
    <EnableLocationBrowseButton>true</EnableLocationBrowseButton>
    <Icon>__TemplateIcon.ico</Icon>
  </TemplateData>
  <TemplateContent>
    <Project TargetFileName="myproject_project.vcxproj" File="myproject_project.vcxproj" ReplaceParameters="true">
      <ProjectItem ReplaceParameters="false" TargetFileName="$projectname$.vcxproj.filters">myproject_project.vcxproj.filters</ProjectItem>
      <ProjectItem ReplaceParameters="true" TargetFileName="myproject_project.cpp">myproject_project.cpp</ProjectItem>
      <ProjectItem ReplaceParameters="false" TargetFileName="ReadMe.txt">ReadMe.txt</ProjectItem>
    </Project>
  </TemplateContent>
  <WizardExtension>
    <Assembly>myprojectvstemplate, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=a0a3d031ed112d61</Assembly>
    <FullClassName>myprojectvstemplate.IWizardImplementation</FullClassName>
  </WizardExtension>
</VSTemplate>

不幸的是,当我尝试创建新的模板项目时,根本不会显示UI。该项目刚刚打开,并且没有替换$ custommessage $参数。

为什么我不能显示向导的GUI?

另外:有没有办法调试为什么没有加载程序集?

2 个答案:

答案 0 :(得分:1)

可能找不到具有向导实现的程序集。

在WizardExtension部分中,您应该准确编写程序集和类名。

当然,此程序集应位于扩展目录中或在GAC中注册。

<WizardExtension>
    <Assembly>myprojectvstemplate, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=a0a3d031ed112d61</Assembly>
    <FullClassName>myprojectvstemplate.IWizardImplementation</FullClassName>
</WizardExtension>
谢谢,谢尔文

答案 1 :(得分:1)

我遇到了与OP相同的问题,遵循相同的教程。

我找到的解决方案是:

修改模板部分中,

步骤2将zip文件解压缩到一个文件夹中,然后删除zip文件。然后修改vstemplate。

最后在VS的新实例中尝试向导。

我不知道这是否是正确的解决方案,但这对我有用。