gpt4 book ai didi

c++ - 向 visual studio 模板添加过滤器

转载 作者:行者123 更新时间:2023-11-28 07:32:38 24 4
gpt4 key购买 nike

我正在尝试使用向导在 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());
}
}

虽然这是失败的。返回的错误是:

System.IndexOutOfRangeException: Index was outside the bounds of the array.

at System.Array.InternalGetReference(Void* elemRef, Int32 rank, Int32* pIndices)

at System.Array.GetValue(Int32 index)

at my_wizard.IMyWizard.RunStarted(Object automationObject, Dictionary`2 replacementsDictionary, WizardRunKind runKind, Object[] customParams)

我哪里错了?如何向 vs 模板添加过滤器?

最佳答案

你可以找到答案here .
有人说当 Solution Explorer 未打开时会出现此问题。
这是我基于上层链接的解决方案:

    private Project getActiveProject(DTE2 dte)
{
Array projects = dte.ActiveSolutionProjects as Array;
if (projects == null || projects.Length == 0)
{
projects = dte.Solution.SolutionBuild.StartupProjects as Array;
if (projects == null || projects.Length == 0)
{
Projects pro = dte.Solution.Projects;
if (pro == null || pro.Count == 0)
return null;
return pro.Item(0);
}
}
return projects.GetValue(0) as Project;
}

关于c++ - 向 visual studio 模板添加过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17366142/

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