Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将筛选器添加到visual studio模板_C++_Visual Studio_Visual Studio 2012_Visual Studio Templates - Fatal编程技术网

C++ 将筛选器添加到visual studio模板

C++ 将筛选器添加到visual studio模板,c++,visual-studio,visual-studio-2012,visual-studio-templates,C++,Visual Studio,Visual Studio 2012,Visual Studio Templates,我正试图使用向导在visual studio(C++)项目模板中创建一个过滤器(一个小文件夹,它只在项目中分隔文件),因此我正在RunStarted方法中编写以下代码: public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams

我正试图使用向导在visual studio(C++)项目模板中创建一个过滤器(一个小文件夹,它只在项目中分隔文件),因此我正在RunStarted方法中编写以下代码:

public void RunStarted(object automationObject,
        Dictionary<string, string> replacementsDictionary,
        WizardRunKind runKind, object[] customParams)
    {
      try
      {
        // Add filters to the project
        EnvDTE.DTE dte = (EnvDTE.DTE)automationObject;

        Array activeProjects = (Array)dte.ActiveSolutionProjects;
        Project activeProj = (Project)activeProjects.GetValue(0);
        VCProject prj = (VCProject)activeProj.ProjectItems.Item(0);
        VCFilter filter = prj.AddFilter("Header_Files");
        filter.AddFile("header.h");
        prj.Save();
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.ToString());
      }
    }
public void运行已启动(对象自动对象,
词典替换词典,
WizardRunKind,对象[]customParams)
{
尝试
{
//将过滤器添加到项目中
EnvDTE.DTE DTE=(EnvDTE.DTE)automationObject;
数组activeProjects=(数组)dte.ActiveSolutionProjects;
Project activeProj=(Project)activeProjects.GetValue(0);
VCProject prj=(VCProject)activeProj.ProjectItems.Item(0);
VCFilter filter=prj.AddFilter(“头文件”);
filter.AddFile(“header.h”);
prj.Save();
}
捕获(例外情况除外)
{
Show(例如ToString());
}
}
但这是失败的。返回的错误为:

System.IndexOutOfRangeException:索引超出了 数组

在System.Array.InternalGetReference(Void*elemRef,Int32秩, Int32*小方块)

位于System.Array.GetValue(Int32索引)

在my_wizard.IMyWizard.RunStarted(objectautomationobject, 字典'2 replacementsDictionary,向导runKind,对象[] 自定义参数)

我哪里做错了?如何向vs模板添加筛选器?

您可以找到答案。
有人说,当解决方案资源管理器未打开时,会出现此问题。
以下是我基于上面链接的解决方案:

私有项目getActiveProject(DTE2 dte)
{
数组项目=dte.ActiveSolutionProjects作为数组;
if(projects==null | | projects.Length==0)
{
projects=dte.Solution.SolutionBuild.StartupProjects作为阵列;
if(projects==null | | projects.Length==0)
{
Projects pro=dte.Solution.Projects;
如果(pro==null | | pro.Count==0)
返回null;
返回专业项目(0);
}
}
返回项目。GetValue(0)作为项目;
}