【问题标题】:VS2012 template wizard - GUI not showingVS2012 模板向导 - GUI 未显示
【发布时间】:2013-06-15 09:08:20
【问题描述】:

我的 Visual Studio 模板向导的 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?

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

【问题讨论】:

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


    【解决方案1】:

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

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

    修改模板部分,

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

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

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

    【讨论】:

    • 我不得不做很多诡计和机械,但我终于让它工作了。谢谢 Paresh,虽然你只是给了我一个提示,但我终于让它工作了。
    • 我不明白这个答案。我在该链接上的相同教程之后遇到了同样的问题,但没有“修改模板”部分,整个页面上也没有提到 zip 文件。
    【解决方案2】:

    可能没有找到带有向导实现的程序集。

    在 WizardExtension 部分,你应该准确地写出你的程序集和类名。

    当然,这个程序集应该在扩展目录中或在GAC中注册。

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

    谢谢,谢尔盖

    【讨论】:

    • 我按照上面的描述用 gacutil.exe 注册了它,没有出现“程序集注册成功”的错误。关于wizardextension,我对Culture 不是100% 确定(大会说“无”),但我认为Neutral 是一回事。令牌是最新的,程序集名称与主命名空间名称相同,fullclassname 是程序集名称。 IWizardImplementation(我的 UI 类)
    • 您可以捕获一些异常,如果有的话:1) 在 VS2012 的第一个实例中打开新项目对话框,2) 打开 VS2012 的第二个实例 3) 将调试器从 VS2012 的第二个实例附加到第一个实例,4)在VS2012的第二个实例中启用异常,5)尝试在VS2012的第一个实例中创建新项目如果有一些异常,您将在VS2012的第二个实例中看到它们
    • 试过了,但除了这个异常之外什么也没得到:“值不在预期范围内”。不幸的是,我不明白它指的是什么范围/值。如果您可以使用模板项目为 vs2012 创建一个简单的向导并将其压缩给我,我将不胜感激,这样我就可以检查哪里出错了。这应该是按照上述步骤完成的 5 分钟任务,并确保我没有出错
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 1970-01-01
    • 2018-09-02
    • 2019-09-03
    • 2015-10-28
    相关资源
    最近更新 更多