简单的问题 - 我找到了两种将工具窗口添加到 Visual Studio (2008) 的方法:创建插件或创建包。
(插件:http: //www.codeproject.com/KB/dotnet/vstoolwindow.aspx)
(包:http: //msdn.microsoft.com/en-us/library/bb165051.aspx)
什么是“正确”的方式?
简单的问题 - 我找到了两种将工具窗口添加到 Visual Studio (2008) 的方法:创建插件或创建包。
(插件:http: //www.codeproject.com/KB/dotnet/vstoolwindow.aspx)
(包:http: //msdn.microsoft.com/en-us/library/bb165051.aspx)
什么是“正确”的方式?
你可以做任何一个,我都做过。在某些方面,插件更容易一些,但它们也有一些烦人的缺点。
加入:
VSPackages:
我认为 280Z28 在 VS2010 之前是完全正确的。但是现在VS2010和VS012:
此外,VS2010 支持另一种可扩展性:那些是 MEF 扩展,它们是轻量级插件,仅在 IDE 的特定事件(如文本编辑器事件)时触发。一个例子是FixMixedTabs扩展。
只需创建一个 VSPackage 空包(无菜单、命令等)并将其复制到主类中以创建一个 VSPackage,它基本上在有活动解决方案时加载,并且只需获取对DTE2
. 通过这种方式,您可以将其用作加载项。
// This attribute tells the PkgDef creation utility (CreatePkgDef.exe) that this class is
// a package.
[PackageRegistration(UseManagedResourcesOnly = true)]
// This attribute is used to register the informations needed to show the this package
// in the Help/About dialog of Visual Studio.
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
[Guid(GuidList.guidVSPackage1PkgString)]
// Load this package when a solution is loaded (VSConstants.UICONTEXT_SolutionExists)
[ProvideAutoLoad("{f1536ef8-92ec-443c-9ed7-fdadf150da82}")]
public sealed class VSPackage1Package : Package
{
/// <summary>
/// Default constructor of the package.
/// Inside this method you can place any initialization code that does not require
/// any Visual Studio service because at this point the package object is created but
/// not sited yet inside Visual Studio environment. The place to do all the other
/// initialization is the Initialize method.
/// </summary>
public VSPackage1Package()
{
Trace.WriteLine(string.Format(CultureInfo.CurrentCulture, "Entering constructor for: {0}", this.ToString()));
}
/// <summary>
/// Initialization of the package; this method is called right after the package is sited, so this is the place
/// where you can put all the initilaization code that rely on services provided by VisualStudio.
/// </summary>
protected override void Initialize()
{
Trace.WriteLine (string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));
base.Initialize();
IVsExtensibility extensibility =
GetService(typeof(EnvDTE.IVsExtensibility)) as
IVsExtensibility;
DTE2 dte = extensibility.GetGlobalsObject(null).DTE as DTE2;
}
}
如果您只是创建一个简单的工具窗口,那么我建议您使用插件路线。
包和加载项都是扩展 Visual Studio 的方式。一般来说,它们具有相同的功能。包更强大一些,可以更深入地集成到 Visual Studio。但是,这种更深层次的集成会带来成本,包括更长的升级时间和安装程序。
加载项被设计为一种更轻量级的扩展机制。启动时间更短,安装更容易。
如果您所做的只是工具窗口与编辑器的基本交互我们的代码模型,那么插件是最好的途径。