没有返回对通过 AddFromTemplate() 创建的项目项的引用

发布于 2024-10-20 04:30:52 字数 461 浏览 6 评论 0原文

在成功创建表单的 Visual Studio 加载项中,但没有提供对 EnvDTE 的 prjItem 实例的引用。

下面是将表单添加到项目中的代码片段:

string templatePath = solution.GetProjectItemTemplate("Form.zip", "csproj");
ProjectItem prjItem = project.ProjectItems.AddFromTemplate(templatePath, "myForm.cs");

Obs.: 'solution' is an EnvDTE80.Solution2 object。

当然,我可以通过其他方式获取引用,例如 proj.ProjectItems.Item([index]) (执行循环并检查名称),但这不是我想要的方式,而且我需要此引用向该窗体​​添加控件。

我做错了什么吗?

In a Visual Studio Add-In that successfully creates a form, but gives me no reference back to the EnvDTE's prjItem instance.

Here's the piece of code that adds the form to the project:

string templatePath = solution.GetProjectItemTemplate("Form.zip", "csproj");
ProjectItem prjItem = project.ProjectItems.AddFromTemplate(templatePath, "myForm.cs");

Obs.: 'solution' is an EnvDTE80.Solution2 object.

Of cource I can get the reference by other ways, like proj.ProjectItems.Item([index]) (doing a loop and checking for names), but that's not how I want to do it, and I need this reference in orther to add controls to this form.

Am I doing something wrong?

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

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

发布评论

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

评论(1

深海蓝天 2024-10-27 04:30:52

刚刚在MSDN上找到一条评论:

AddFromTemplate 始终返回 NULL

曾经,这是真的。但
使用更高版本的 Visual Studio,
其中包括添加的能力
单个模板中的多个项目,
该方法的返回值可以
不退回多件商品。所以现在
在所有实例中返回 NULL 值。
这是由于以下条件的限制
此特定的 COM 签名
方法不能改变,除非
破坏了许多已经在使用的代码。

因此,如果您需要
该项目的ProjectItem接口而已
通过 AddFromTemplate 调用添加,
你可以迭代
ProjectItems 集合,或者您可以
创建一个 ProjectItemsEvents.ItemAdded
通话前发生的事件
AddFromTemplate,并存储
ProjectItem 传递给您的 OnItemAdded
处理程序。

http://msdn。 microsoft.com/en-us/library/envdte.projectitems.addfromtemplate(v=vs.80).aspx#1

Just found a comment on MSDN:

AddFromTemplate always returns a NULL
value

At one time, this was true. But
with later versions of Visual Studio,
which included the ability to add
multiple items from a single template,
the return value for this method could
not return multiple items. So it now
returns a NULL value in all instances.
This is due to the contraint that the
COM signature for this particular
method cannot be changed without
breaking a lot of code already in use.

Consequently, if you need the
ProjectItem interface of the item just
added via the AddFromTemplate call,
you can either iterate through the
ProjectItems collection, or you can
create a ProjectItemsEvents.ItemAdded
event just before calling
AddFromTemplate, and store away the
ProjectItem passed to your OnItemAdded
handler.

http://msdn.microsoft.com/en-us/library/envdte.projectitems.addfromtemplate(v=vs.80).aspx#1

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