如何通过 Visual Studio 中的加载项添加 jscript 或 html 项模板

发布于 2024-09-27 02:55:11 字数 734 浏览 0 评论 0原文

我当前正在开发一个加载项,并且需要以编程方式将自定义项模板添加到项目的功能。

我当前正在 ..\Visual Studio 2010\Templates\ItemTemplates\ 中安装模板(采用 .zip 格式)。

然后,我在加载项中调用以下代码:

string templateLocation = (solution as Solution2).GetProjectItemTemplate(templateName, language);
projectToAddTo.ProjectItems.AddFromTemplate(templateLocation, nameOfNewItem);

如果项目模板适用于 C# 文件,则如果我使用“CSharp”作为语言变量并将模板放置在语言文件夹“Visual C#”或“Visual Web Developer”中\Visual C#" 那么这一切都工作正常;在 Visual Studio 中正确发现并实施模板。
但是,如果我创建 JScript 或 HTML 模板,并且将语言提供为“JScript”或“HTML”,则 GetProjectItemTemplate 方法会引发异常。我尝试将模板放置在“ItemTemplates\JScript(HTML)”和“ItemTemplates\Visual Web Devlerop\JScript(HTML)”中,但没有成功。

任何人都知道我做错了什么,这些自定义项目模板支持哪些语言,语言参数和文件夹位置应该是什么?

I'm currenlty working on an add-in and require the functionality to add custom item templates to a project programatically.

I'm currently installing the templates (which are of a .zip format) in ..\Visual Studio 2010\Templates\ItemTemplates\.

From within the add-in I then call the following code:

string templateLocation = (solution as Solution2).GetProjectItemTemplate(templateName, language);
projectToAddTo.ProjectItems.AddFromTemplate(templateLocation, nameOfNewItem);

If the item template is for a C# file then if i use "CSharp" for the language variable and place the template in the Language Folder "Visual C#" or "Visual Web Developer\Visual C#" then this all works fine; the template is discovered and implemented correctly within Visual Studio.
However if I create a JScript or HTML template, the GetProjectItemTemplate method throws an exception if I supply the language as "JScript" or "HTML". I've tried placing the templates in both "ItemTemplates\JScript(HTML)" and "ItemTemplates\Visual Web Develerop\JScript(HTML)" with no luck.

Anyone have any ideas about what I'm doing wrong, what languages are supported for these custom item templates, what the language parameter and folder location should be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒江雪… 2024-10-04 02:55:11

进一步研究后发现,string Solution2.GetProjectItemTemplate(string TemplateName, string Language) 中的参数 Language 实际上是项模板所针对的项目的语言类型。因此,无论模板中的实际项目是 java 文件还是 html 文件,项目仍然是 CSharp 或 VisualBasic,因此这就是应该作为 Language 参数传递的内容。

此外,在自定义项和项目模板的位置中,文件夹的名称并不重要,无论是 Visual C#、Visual Basic、Visual Web Developer\JScript。文件夹的名称只会影响它在“添加新项目”对话框中的显示方式以及在缓存中的存储方式。

此外,调用 GetProjectItemTemaplate 时指定的 TemplateName 参数应具有 ItemTemplates 文件夹下的完整相关路径。尽管没有它它也可以工作,但如果您在不同位置有多个同名模板,那么您将获得 VS 返回的第一个模板。

From having looked into this a bit more it seems that the parameter Language in string Solution2.GetProjectItemTemplate(string TemplateName, string Language) is actually the language type of the project which the item template is targetting. Hence whether the actual item in the template is a java file or an html file, the project would still be CSharp or VisualBasic and so that is what should be passed in as the Language parameter.

Furthermore, in the location for custom item and project templates, it doesnt matter what the folder is called, be it Visual C#, Visual Basic, Visual Web Developer\JScript. The name of the folders just affects how it appears in the Add New Item Dialog and how its stored in the cache.

Also, the TemplateName parameter you specify when calling GetProjectItemTemaplate should have the full reletive path from beneath the ItemTemplates folder. Although it will work without it, if you have more than one template with the same name in different locations then you'll get the first one returned by VS.

愚人国度 2024-10-04 02:55:11

我的建议是采用 HTML 或 JS 并使用 VS 自己的菜单将其导出为模板。然后,解压缩模板并在 .vstemplate 文件中查找 language 参数。

My suggestion is to take an HTML or JS and use VS's own menu to export this as a template. Then, unzip the template and look for the language parameter in the .vstemplate file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文