今天研究了一下vs 2008 package的部署,即怎样放置package中的dll、ProjectTemplate和ItemTemplate,趁着热乎劲先记录下来,免得以后忘记。
首先,创建一个简单的package,供测试之用。
打开visual studio 2008,新建项目,选择其他项目类型->扩展性->Visual Studio Integration Package,输入项目名称为DeployPackage,如下图:
点击确定后,弹出创建package的向导,选择C#作为开发语言,选中Menu Command。如下两图:
完成向导后,vs帮我们创建了空的package项目,如下图:
双击Guids.cs文件,添加一个guid,作为接下来要创建的ProjectFactory的guid,修改后的Guids.cs文件如下:
1: using System;
2:
3: namespace Company.DeployPackage
4: {
5: static class GuidList
6: {
7: public const string guidDeployPackagePkgString = "bad3390c-b2a2-4bfc-a3ad-87e8119df413";
8: public const string guidDeployPackageCmdSetString = "b09083be-4856-4364-8bb1-243d133c7177";
9:
10:
11: //该Guid为接下来要创建的ProjectFactory的guid
12: public const string guidDixProjectFactoryString = "9819EC75-61CC-468c-9823-E853AEE0FE8C";
13:
14: public static readonly Guid guidDeployPackageCmdSet = new Guid(guidDeployPackageCmdSetString);
15: };
16: }
新增一个ProjectFactory,名为DixProjectFactory,如下:
1: using System;
2: using System.Runtime.InteropServices;
3: using Microsoft.VisualStudio.Project;
4: using IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
5:
6: namespace Company.DeployPackage
7: {
8: //注意这个guid
9: [Guid(GuidList.guidDixProjectFactoryString)]
10: public class DixProjectFactory : ProjectFactory
11: {
12: public DixProjectFactory(DeployPackagePackage package)
13: : base(package)
14: {
15: }
16:
17: protected override ProjectNode CreateProject()
18: {
19: DixProjectNode node = new DixProjectNode();
20: node.SetSite((IOleServiceProvider)((IServiceProvider)Package).GetService(typeof(IOleServiceProvider)));
21: return node;
22: }
23: }
24: }
注意DixProjectFactory的Guid就是刚刚在Guids.cs文件中的那个Guid。
注意:需要下载MPFProjectBase才能使用ProjectFactory,以及下面的ProjectNode、ProjectPackage
新增一个ProjectNode类,名为DixProjectNode,如下:
1: using System;
2: using Microsoft.VisualStudio.Project;
3:
4: namespace Company.DeployPackage
5: {
6: public class DixProjectNode : ProjectNode
7: {
8: /// <summary>
9: /// 返回DixProjectFactory的Guid
10: /// </summary>
11: public override Guid ProjectGuid
12: {
13: get { return typeof(DixProjectFactory).GUID; }
14: }
15:
16: public override string ProjectType
17: {
18: get { return "Dix"; }
19: }
20: }
21: }
修改DeployPackagePackage类,使它继承于ProjectPackage(原来是继承与Package的),并添加一个ProvideProjectFactoryAttribute,如下:
1: ...
2: [ProvideProjectFactory(typeof(DixProjectFactory), "我的dix项目", "Dix Project Files (*.dixproj);*.dixproj", "dixproj", "dixproj", "", LanguageVsTemplate = "DiX")]
3: public sealed class DeployPackagePackage : ProjectPackage
4: {
5: ...
6: }
修改DeployPackagePackage类的Initialize方法,添加RegisterProjectFactory(new DixProjectFactory(this))语句,修改后的Initialize方法如下:
1: protected override void Initialize()
2: {
3: Trace.WriteLine (string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString()));
4: base.Initialize();
5:
6: // Add our command handlers for menu (commands must exist in the .vsct file)
7: OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
8: if ( null != mcs )
9: {
10: // Create the command for the menu item.
11: CommandID menuCommandID = new CommandID(GuidList.guidDeployPackageCmdSet, (int)PkgCmdIDList.cmdidMyCommand);
12: MenuCommand menuItem = new MenuCommand(MenuItemCallback, menuCommandID );
13: mcs.AddCommand( menuItem );
14: }
15:
16: //注册ProjectFactory
17: RegisterProjectFactory(new DixProjectFactory(this));
18: }
下面为该package添加ProjectTemplate和ItemTemplate。
添加Templates、Projects、Items等文件夹和文件,添加后如下图:
注意这里面的cs文件需要改一下Build Action,否则编译不过。也可以把这堆文件的Build Action设置成ZipProject和ZipItem,但为了更加清楚模板的部署方式,我只是把他们的Build Action设置成了None。
Projects文件夹中放置Project模板,Items文件夹中放置ProjectItem模板。
Project模板以WinForm为例,要注意WinForm.vstemplate文件,该文件的定义如下:
<VSTemplate Version="2.0.0" Type="Project" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005"> <TemplateData> <Name>DiX WinForm Application</Name> <Description>Create Dix WinForm Application</Description> <Icon>WinForm.ico</Icon> <!--ProjectType要和Package里的LanguageVsTemplate相同--> <ProjectType>DiX</ProjectType> <SortOrder>20</SortOrder> <NumberOfParentCategoriesToRollUp>1</NumberOfParentCategoriesToRollUp> <CreateNewFolder>true</CreateNewFolder> <DefaultName>WinFormApplication</DefaultName> <ProvideDefaultName>true</ProvideDefaultName> <PromptForSaveOnCreation>false</PromptForSaveOnCreation> </TemplateData> <TemplateContent> <Project File="WinForm.dixproj" TargetFileName="$safeprojectname$.dixproj" ReplaceParameters="true"> <ProjectItem ReplaceParameters="true" OpenInEditor="true" TargetFileName="Form1.dix">Form1.dix</ProjectItem> </Project> </TemplateContent></VSTemplate>
ProjectItem模板以Class为例,要注意Class.vstemplate文件,该文件的定义如下:
<?xml version="1.0" encoding="utf-8"?><VSTemplate Version="3.0.0" Type="Item" xmlns="http://schemas.microsoft.com/developer/vstemplate/2005"> <TemplateData> <Name Package="{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}"> ID="2245" /> <Description Package="{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}" ID="2262" /> <Icon Package="{FAE04EC1-301F-11d3-BF4B-00C04F79EFBC}" ID="4515" /> <TemplateID>Dix.Class</TemplateID> <ProjectType>Dix</ProjectType> <RequiredFrameworkVersion>2.0</RequiredFrameworkVersion> <NumberOfParentCategoriesToRollUp>1</NumberOfParentCategoriesToRollUp> <DefaultName>Class.cs</DefaultName> </TemplateData> <TemplateContent> <ProjectItem ReplaceParameters="true">Class.cs</ProjectItem> </TemplateContent> </VSTemplate>
来源:https://www.cnblogs.com/default/archive/2010/01/20/1652669.html