Visual Studio 项目模板制作(四)

上一篇,介绍了VSIX安装模板的方法,那么,你是不是要问,为何有些项目模板却可以有向导,那是怎么做到的

今天这篇文章就是介绍如何为自己的模板添加向导,向导可以引导你完成项目中各种参数的设置,比如项目创建人,项目描述,公司等

下图创建Web项目时的向导

下面我们开始制作我们自己的项目向导

需要用到的两个类库: envdte , Microsoft.VisualStudio.TemplateWizardInterface

创建一个类库项目,引用上面两个类库

添加一个类,取名ProjectWizard继承IWizard接口实现 RunStarted 方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EnvDTE;
using Microsoft.VisualStudio.TemplateWizard;

namespace TemplateTestWizard
{
    public class ProjectWizard:IWizard
    {
        private bool shouldAdd = true;
        public string Creator = "";
        public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            Creator = replacementsDictionary["$username$"];
            AssemblyWizard wizard=new AssemblyWizard(this);
            if (wizard.ShowDialog() == DialogResult.OK)
            {
                replacementsDictionary["$username$"]=Creator;
                shouldAdd = true;
            }
            else
            {
                shouldAdd = false;
            }
        }

        public void ProjectFinishedGenerating(Project project)
        {

        }

        public void ProjectItemFinishedGenerating(ProjectItem projectItem)
        {

        }

        public bool ShouldAddProjectItem(string filePath)
        {
            return shouldAdd;
        }

        public void BeforeOpeningFile(ProjectItem projectItem)
        {

        }

        public void RunFinished()
        {

        }
    }
}

下面为项目添加签名,由于制作的类库需要放到全局应用缓存中去,必须要添加签名

然后,打开Visual Studio命令提示符,以管理员运行,用 gacutil -i  注册生成的dll

gacutil -i path 注册程序集到全局应用程序缓存中
gacutil -l name  查看相应的程序集
gacutil -u name 卸载相应的程序集

下面,修改前面制作的Template,解压Template文件,打开 .vstemplate 文件,添加如下代码

<WizardExtension>
    <Assembly>
      TemplateTestWizard, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2be8376f02202422, processorArchitecture=MSIL
    </Assembly>
    <FullClassName>TemplateTestWizard.ProjectWizard</FullClassName>
  </WizardExtension>

其中 Assembly 节点写的是内容可以用 gacutil -l name 查看,然后复制出来

FullClassName 是项目中ProjectWizard的完全限定名称

修改完模板后,在VS中打开新建项目

这个自定义向导就出来了,

如果模板里面有 $username$

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

namespace $safeprojectname$
{
    /// <summary>
    /// 创建人:$username$
    /// </summary>
    public static class CodeTimer
    {
        public static void Initialize()
        {
            Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
            Thread.CurrentThread.Priority = ThreadPriority.Highest;
            Time("", 1, () => { });
        }
        public static void Time(string name, int iteration, Action action)
        {
            if (String.IsNullOrEmpty(name)) return;

            // 1.
            ConsoleColor currentForeColor = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(name);

            // 2.
            GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
            int[] gcCounts = new int[GC.MaxGeneration + 1];
            for (int i = 0; i <= GC.MaxGeneration; i++)
            {
                gcCounts[i] = GC.CollectionCount(i);
            }

            // 3.
            Stopwatch watch = new Stopwatch();
            watch.Start();
            ulong cycleCount = GetCycleCount();
            for (int i = 0; i < iteration; i++) action();
            ulong cpuCycles = GetCycleCount() - cycleCount;
            watch.Stop();

            // 4.
            Console.ForegroundColor = currentForeColor;
            Console.WriteLine("\tTime Elapsed:\t" + watch.ElapsedMilliseconds.ToString("N0") + "ms");
            Console.WriteLine("\tCPU Cycles:\t" + cpuCycles.ToString("N0"));

            // 5.
            for (int i = 0; i <= GC.MaxGeneration; i++)
            {
                int count = GC.CollectionCount(i) - gcCounts[i];
                Console.WriteLine("\tGen " + i + ": \t\t" + count);
            }

            Console.WriteLine();
        }

        private static ulong GetCycleCount()
        {
            ulong cycleCount = 0;
            QueryThreadCycleTime(GetCurrentThread(), ref cycleCount);
            return cycleCount;
        }

        [DllImport("kernel32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool QueryThreadCycleTime(IntPtr threadHandle, ref ulong cycleTime);

        [DllImport("kernel32.dll")]
        static extern IntPtr GetCurrentThread();
    }
}

生成之后代码

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

namespace ConsoleApplication25
{
    /// <summary>
    /// 创建人:Administrator
    /// </summary>
    public static class CodeTimer
    {
        public static void Initialize()
        {
            Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
            Thread.CurrentThread.Priority = ThreadPriority.Highest;
            Time("", 1, () => { });
        }
        public static void Time(string name, int iteration, Action action)
        {
            if (String.IsNullOrEmpty(name)) return;

            // 1.
            ConsoleColor currentForeColor = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(name);

            // 2.
            GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
            int[] gcCounts = new int[GC.MaxGeneration + 1];
            for (int i = 0; i <= GC.MaxGeneration; i++)
            {
                gcCounts[i] = GC.CollectionCount(i);
            }

            // 3.
            Stopwatch watch = new Stopwatch();
            watch.Start();
            ulong cycleCount = GetCycleCount();
            for (int i = 0; i < iteration; i++) action();
            ulong cpuCycles = GetCycleCount() - cycleCount;
            watch.Stop();

            // 4.
            Console.ForegroundColor = currentForeColor;
            Console.WriteLine("\tTime Elapsed:\t" + watch.ElapsedMilliseconds.ToString("N0") + "ms");
            Console.WriteLine("\tCPU Cycles:\t" + cpuCycles.ToString("N0"));

            // 5.
            for (int i = 0; i <= GC.MaxGeneration; i++)
            {
                int count = GC.CollectionCount(i) - gcCounts[i];
                Console.WriteLine("\tGen " + i + ": \t\t" + count);
            }

            Console.WriteLine();
        }

        private static ulong GetCycleCount()
        {
            ulong cycleCount = 0;
            QueryThreadCycleTime(GetCurrentThread(), ref cycleCount);
            return cycleCount;
        }

        [DllImport("kernel32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool QueryThreadCycleTime(IntPtr threadHandle, ref ulong cycleTime);

        [DllImport("kernel32.dll")]
        static extern IntPtr GetCurrentThread();
    }
}
时间: 2024-07-31 01:45:23

Visual Studio 项目模板制作(四)的相关文章

Visual Studio 项目模板制作(三)

前面,我们已经制作好了模板,然后放到相应的Template目录就可以在Visual Studio中使用 本篇,我们采用安装VSIX扩展的方式来安装模板,这种方式需要安装Visual Studio SDK 安装了SDK之后,可以在新建项目里面看到VSIX Project 选择VSIX Project 然后设置一下名称,点击确定,项目就新建完成了 项目结构: 现在我们开始 首先,双击打开source.extension.vsixmanifest 设置扩展的各种属性 然后,添加我们前面两篇制作的模板

Visual Studio 项目模板制作(一)

我们编写项目的时候,很多时候都是在写重复代码,比如一个比较完整的框架,然后下面有很多代码都是重复的Copy,其实我们可以利用Visual Studio的模板替我们干这些活,我们只要关注项目具体的业务就可以了: 下面我们开始: 1.模板类别 项目模板.项模板 其中,项目模板是创建项目用的,项模板是创建项用的 项目模板: 项模板: 下面我们创建项目模板 首先,将要制作成模板的项目打开,选中项目,点击文件->导出项目模板,弹出导出模板向导 然后填写相关信息,点击完成,这样就导出模板成功了 现在,很关键

Visual Studio 项目模板制作(二)

上一篇,我们制作了项目模板,本篇我制作项模板 首先,从我们需要导出模板的项目中,文件->导出模板,弹出 导出模板向导 对话框 选择项模板,点击下一步 选择要导出的项,点击下一步 选择要Reference的类库 修改模板名称,点击完成 然后,解压生成的zip文件,如图 打开.vstemplate文件 <VSTemplate Version="3.0.0" xmlns="http://schemas.microsoft.com/developer/vstemplate

创建Visual studio项目模板 vstemplate关键点纪要

from:http://www.cnblogs.com/stickman/p/3454719.html 经过多次的实验,终于完美生成一个.VSIX的项目模板安装包,其中遇到不少问题与挫折,久经google/baidu/自行摸索.终于解决所有问题. 现将心得记录总结如下 关于.vstemplate 1.可以通过导出模板直接生成.vstemplate及其他项目文件,以作为 模板的母版! 建议复制一份csprj文件做.vstemplate里面引用的模板项目文件. 2.TargetFileName/Ta

Visual Studio for Mac第四预

微软发布Visual Studio for Mac第四预览版 去年 11 月,微软发布了 Visual Studio for Mac 的首个预览版本,并且承诺后续数月会带来更多功能.而今天,随着 Visual Studio 2017 的正式发布,Visual Studio for Mac 也迎来了第四个预览版本.Xamarin 团队的 Miguel de Icaza 解释到:"Visual Studio for Mac Preview 4 增添了许多新功能,包括 Xamarin 和 .NET C

[Cordova] 无法编译Visual Studio项目里Plugin副本的Native Code

[Cordova] 无法编译Visual Studio项目里Plugin副本的Native Code 问题情景 开发Cordova Plugin的时候,开发的流程应该是: 建立Cordova Plugin 发布到本机文件系统或是Git服务器 使用Visual Studio挂载Plugin 编译并执行项目 在这个开发的过程中,如果在编译并执行项目的这个步骤,发现Plugin的Native Code需要修正.直觉的想法,会是直接修改Cordova项目里Plugin副本的Native Code之后,再

使用GitHub For Windows部署Visual Studio项目

因为最近同时再看很多技术方面的书,书上的例子有很多自己想亲自尝试一下,但是每次写例子都得创建一个新项目未免太麻烦,索性就整理一个合集,然后发布到GitHub上. 首先使用GitHub For Windows,点击左上角的[+]号,默认的选项就是[Create],选择一个目录,并且将项目名称填入[Name]文本框,如图所示 这样Git项目创建好之后,选择右上角的[Publish Repository],可以写上项目的说明[Description],然后就可以点击下面的[Publish]按钮发布到G

visual studio 项目工程中相对目录

最近从TFS拿到一个很久没有人维护的项目,老是提示dll找不到. 弱弱地研究了一下,原来是相对路径惹的祸. 1. C#中相对路径的表示:. 表示当前目录,..表示上一级目录 2. 工程中的引用 <Reference Include="XXXX.Data"> <HintPath>..\..\..\..\..\..\..\Common\XX\XX\XX\v1.1.0.1105\XXXX.Data.dll</HintPath> </Reference

visual studio 2010模板修改

 使用visual studio 2010好久了,也遇到了不少问题,下面跟大家分享一些. 模板修改 说明: 主要工具: 以visual studio 2010作为例子,具体目录可能会根据不同的安装目录有所区别,如有问题,可以留言 最好关闭正在运行着的visual studio 主要目的: 修改新添加文件(aspx master等等)的初始化模板 修改新添加项目时的所有页面或文件的模板 步骤:  1.找到路径 项目模板修改 D:\Program Files (x86)\Microsoft Visu