上一篇,介绍了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("", , () => { });
}
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 + ];
for (int i = ; i <= GC.MaxGeneration; i++)
{
gcCounts[i] = GC.CollectionCount(i);
} // 3.
Stopwatch watch = new Stopwatch();
watch.Start();
ulong cycleCount = GetCycleCount();
for (int i = ; 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 = ; 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 = ;
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("", , () => { });
}
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 + ];
for (int i = ; i <= GC.MaxGeneration; i++)
{
gcCounts[i] = GC.CollectionCount(i);
} // 3.
Stopwatch watch = new Stopwatch();
watch.Start();
ulong cycleCount = GetCycleCount();
for (int i = ; 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 = ; 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 = ;
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();
}
}

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

  1. Visual Studio 项目模板制作(一)

    我们编写项目的时候,很多时候都是在写重复代码,比如一个比较完整的框架,然后下面有很多代码都是重复的Copy,其实我们可以利用Visual Studio的模板替我们干这些活,我们只要关注项目具体的业务就 ...

  2. Visual Studio 项目模板制作(三)

    前面,我们已经制作好了模板,然后放到相应的Template目录就可以在Visual Studio中使用 本篇,我们采用安装VSIX扩展的方式来安装模板,这种方式需要安装Visual Studio SD ...

  3. Visual Studio 项目模板制作(二)

    上一篇,我们制作了项目模板,本篇我制作项模板 首先,从我们需要导出模板的项目中,文件->导出模板,弹出 导出模板向导 对话框 选择项模板,点击下一步 选择要导出的项,点击下一步 选择要Refer ...

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

    from:http://www.cnblogs.com/stickman/p/3454719.html 经过多次的实验,终于完美生成一个.VSIX的项目模板安装包,其中遇到不少问题与挫折,久经goog ...

  5. Visual Studio项目模板与向导开发

    在[Xamarin+Prism开发详解系列]里面经常使用到[Prism unity app]的模板创建Prism.Forms项目: 备注:由于Unity社区已经不怎么活跃,下一个版本将会有Autofa ...

  6. asp.net core web 解决方案多项目模板制作打包总结

    一.文件夹\项目结构 1.1.文件夹 net6.0:针对.net 6.0 项目模板 net6.0pack:针对net6.0打包 1.2.项目结构 Web\WebApi多项目.各层项目.单元测试项目 目 ...

  7. Visual Studio for Mac第四预

    微软发布Visual Studio for Mac第四预览版 去年 11 月,微软发布了 Visual Studio for Mac 的首个预览版本,并且承诺后续数月会带来更多功能.而今天,随着 Vi ...

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

    [Cordova] 无法编译Visual Studio项目里Plugin副本的Native Code 问题情景 开发Cordova Plugin的时候,开发的流程应该是: 建立Cordova Plug ...

  9. 因GIT默认忽略.dll文件导致的Visual Studio项目通过Bamboo编译失败

    背景 由GIT管理的Visual Studio项目,使用Stash管理远端代码库,通过与Stash集成的Bamboo生成项目并发布 现象 Visual Studio项目本地生成成功,用SourceTr ...

随机推荐

  1. 如何修改WordPress网站默认登录地址wp-admin

    使用过WordPress程序建网站的学员都知道,我们使用Wordpress建好的网站,它的网站登录后台就是“网站域名/wp-admin”.如下图: 为了网站安全,如何修改Wordpress网站默认登录 ...

  2. [py]编码-强力理解版

    py编码骨灰级总结 思路: python执行py文件步骤--py2/3定义变量时unicode差异 1,py2 py3执行py文件的步骤 2,py2 定义变量x='mao' 1.x='mao', # ...

  3. JSON—序列化

    表单数据的序列化   用SerializeArray()将有效控件序列化为JSON对象数组? 包含name和value两个属性 SerializeArray()检测一组表单元素中的有效控件? 1.没有 ...

  4. Nuget的学习总结

    Nuget的学习总结 今天研究了一下nuget,发现nuget实在是太有用了,便写下了这篇博客,希望记录一下自己的学习历程,也希望技术圈的朋友看到之后,如果里面哪里写的不够好,可以给我些宝贵的意见,以 ...

  5. Js基础知识7-Es6新增对象Map和set数据结构

    前言 JavaScript中对象的本质是键值对的集合,ES5中的数据结构,主要是用Array和Object,但是键只能是字符串.为了弥补这种缺憾,ES6带来了一种新的数据结构Map. Map也是键值对 ...

  6. 利用构造函数对canvas里面矩形与扇形的绘制进行一个封装

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Ubuntu系统下使用Jenkins进行项目的自动构建还是项目回滚方法

    上面虽然实现了项目的自动部署,但是有时部署失败的时候我们需要回滚到指定版本的构建,这样才能更灵活的进行项目的构建部署.我们可以选择“参数化的构建过程”进行传递不同的参数来选择是进行新的构建还是回滚 如 ...

  8. 远程获得的有趣的linux命令

    使用这些工具从远程了解天气.阅读资料等. 我们即将结束为期 24 天的 Linux 命令行玩具日历.希望你有一直在看,如果没有,请回到开始,从头看过来.你会发现 Linux 终端有很多游戏.消遣和奇怪 ...

  9. 异常和TCP通讯

    第七章 异常处理 * 异常处理机制中的try-catch * 语法: * try{ * 代码片段 * }catch(XXXException e){ * 当try中的代码片段出现了XXXExcepti ...

  10. Python入门之实现简单的购物车功能

    Talk is cheap,Let's do this! product_list = [ ['Iphone7 Plus', 6500], ['Iphone8 ', 8200], ['MacBook ...