我不明白为什么AddFromTemplate返回null。 [英] I dont understeand why AddFromTemplate returns null .

查看:84
本文介绍了我不明白为什么AddFromTemplate返回null。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先在我的代码中提出一个问题。 



  DTE2 dte2 = ServiceProvider.GetService(typeof(DTE))为DTE2;

            Solution2 solution2 = dte2.Solution as Solution2;

            string projectName =" MyProject4";

            string projectFullPath =  System.IO.Path.GetDirectoryName(solution2.FullName)+ @" \" + projectName;

            string templatePath = solution2.GetProjectTemplate(" ConsoleApplication.zip"," CSharp");

           项目destProject = solution2.AddFromTemplate(templatePath,projectFullPath,projectName,false);



  ;           string itemName =" View.cs";

            string itemTemplatePath = solution2.GetProjectItemTemplate(" CodeFile"," CSharp");

            if(destProject == null)

                  抛出新的ArgumentNullException(" destProject不应为null);



            destProject.ProjectItems.AddFromTemplate(itemTemplatePath,itemName);


当我执行上面的代码时,我可以看到项目已成功创建。 


问题是,从AddFromTemplate获取的Project实例为空。 


< p style ="">我不明白这种情况。你能解释为什么项目实例(destProject)为空, 


我该如何解决?

解决方案

根据
文档
,这总是返回NULL。


原因是模板可能会生成多个项目或多个项目。曾几何时(许多版本回来),这很有用。但是当添加对多个项目/项目的支持时,原始开发人员选择不修改底层的COM自动化
接口,而是选择仅开始返回NULL。


找到刚刚添加的项目或项目的其他方法。例如,您可以搜索项目集合(DTE.Solution.Projects),或者如果您实现自定义IWizard,您的

IWizard.ProjectFinishedGeneration
()将通过EnvDTE.Project。


此致


Let me first ask one issue in my code. 

  DTE2 dte2 = ServiceProvider.GetService(typeof(DTE)) as DTE2;
            Solution2 solution2 = dte2.Solution as Solution2;
            string projectName = "MyProject4";
            string projectFullPath =  System.IO.Path.GetDirectoryName(solution2.FullName) +@"\"+ projectName;
            string templatePath = solution2.GetProjectTemplate("ConsoleApplication.zip", "CSharp");
            Project destProject = solution2.AddFromTemplate(templatePath, projectFullPath, projectName, false);

            string itemName = "View.cs";
            string itemTemplatePath = solution2.GetProjectItemTemplate("CodeFile", "CSharp");
            if (destProject == null)
                throw new ArgumentNullException("destProject should not be null");

            destProject.ProjectItems.AddFromTemplate(itemTemplatePath, itemName);

When I executed the above code, I could see the project was created successfully. 

the problem is, however, the Project instance obtained from AddFromTemplate was null. 

I don't understand this situation. can you explain why the project instance(destProject) is null, 

and how can I fix it?

解决方案

Per the documentation, this always returns a NULL.

The reason is that a template may produce multiple items, or multiple projects. Once upon a time (many versions back), this worked. But when support for multiple items/projects was added, the original developers opted not to revise the underlying COM automation interfaces, and instead opted to just start returning NULL.

There are other ways to find the project or item just added. For example, you can search the collection of Projects (DTE.Solution.Projects), or if you implement a custom IWizard, your IWizard.ProjectFinishedGeneration() will be passed the EnvDTE.Project.

Sincerely,


这篇关于我不明白为什么AddFromTemplate返回null。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆