gpt4 book ai didi

c# - 未返回对通过 AddFromTemplate() 创建的项目项的引用

转载 作者:太空狗 更新时间:2023-10-29 22:55:04 24 4
gpt4 key购买 nike

在成功创建表单的 Visual Studio 加载项中,但没有给我返回 EnvDTE 的 prjItem 实例的引用。

这是将表单添加到项目的代码片段:

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

观察:“解决方案”是一个 EnvDTE80.Solution2 对象。

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

我做错了什么吗?

最佳答案

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

AddFromTemplate always returns a NULLvalue

At one time, this was true. Butwith later versions of Visual Studio,which included the ability to addmultiple items from a single template,the return value for this method couldnot return multiple items. So it nowreturns a NULL value in all instances.This is due to the contraint that theCOM signature for this particularmethod cannot be changed withoutbreaking a lot of code already in use.

Consequently, if you need theProjectItem interface of the item justadded via the AddFromTemplate call,you can either iterate through theProjectItems collection, or you cancreate a ProjectItemsEvents.ItemAddedevent just before callingAddFromTemplate, and store away theProjectItem passed to your OnItemAddedhandler.

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

关于c# - 未返回对通过 AddFromTemplate() 创建的项目项的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5184311/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com