c# - VS2012 模板向导 - GUI 未显示

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

我在显示 Visual Studio 模板向导的图形用户界面时遇到问题。我遵循了以下步骤:http://msdn.microsoft.com/en-us/library/ms185301.aspx

这是我做的:

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

用户输入表单.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?

另外:有什么方法可以调试程序集未加载的原因吗??

最佳答案

按照相同的教程,我遇到了与 OP 相同的问题。

我找到的解决方案是这样的:

修改模板部分,

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

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

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

关于c# - VS2012 模板向导 - GUI 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17121986/

相关文章:

c# - 如何继承字符串类?

c# - 为什么 "-"显示为问号?

c# - 通过 TCP 套接字发送命令的最佳方式是什么?

c# - 保存然后访问保存的文件

c++ - 哪些编译器选项用于在 Visual Studio 中编译 C++ STL 类/函数?

c# - 在运行时自定义菜单项快捷键

visual-studio - 如何在 TeamCity 构建时从 Nuget 包中引用而不是复制 js 文件?

c++ - std::put_time 的 MSVC 实现

c# - HTTPWebResponse 响应字符串被截断

c# - Net Core 3.0 中没有 UseDatabaseErrorPage() 扩展方法