visual-studio - 在 VS 2017 自定义项目系统中,如何在解决方案资源管理器中将项目项设为粗体?

标签 visual-studio visual-studio-2017 vs-extensibility extensibility common-project-system

我正在为 VS 2017 编写一个项目系统扩展,我的语言中的每个项目都有一个文件,即“启动文件”。我希望该文件在解决方案资源管理器中显示为粗体。

Python Tools for VS做我正在寻找的东西,但我的扩展是建立在新的项目系统框架 (CPS) 上的。更改解决方案资源管理器项目外观的 CPS 方法是实现 IProjectTreePropertiesProvider ,但我看不出有任何方法可以用它更改文本样式——只有图标。

最佳答案

我不确定 CPS 是否为此内置了任何内容,但您仍然可以使用“旧” native /托管 Visual Studio 界面的组合。这是一个使用 IProjectTreePropertiesProvider 的示例:

[Export(typeof(IProjectTreePropertiesProvider))]
[AppliesTo(MyUnconfiguredProject.UniqueCapability)]
[Order(1000)]
internal class ProjectTreePropertiesProvider1 : IProjectTreePropertiesProvider
{
    // we need to import that to do COM calls
    [Import]
    protected IProjectThreadingService ThreadingService { get; set; }

    // we want the "old" IVsHierarchy interface 
    [ImportMany(ExportContractNames.VsTypes.IVsHierarchy)]
    private OrderPrecedenceImportCollection<IVsHierarchy> IVsHierarchies { get; }
    private IVsHierarchy VsHierarchy => IVsHierarchies.First().Value;

    [ImportingConstructor]
    public ProjectTreePropertiesProvider1(UnconfiguredProject unconfiguredProject)
    {
        IVsHierarchies = new OrderPrecedenceImportCollection<IVsHierarchy>(projectCapabilityCheckProvider: unconfiguredProject);
    }

    /// <summary>
    /// Calculates new property values for each node in the project tree.
    /// </summary>
    /// <param name="propertyContext">Context information that can be used for the calculation.</param>
    /// <param name="propertyValues">Values calculated so far for the current node by lower priority tree properties providers.</param>
    public async void CalculatePropertyValues(IProjectTreeCustomizablePropertyContext propertyContext, IProjectTreeCustomizablePropertyValues propertyValues)
    {
        // this is from the standard WindowsScript project type sample
        if (propertyValues.Flags.Contains(ProjectTreeFlags.Common.ProjectRoot))
        {
            // etc..
            propertyValues.Icon = KnownMonikers.JSProjectNode.ToProjectSystemType();
            // etc..
        }

        // now, we're doing some COM calls, ensure it happens on UI thread
        await ThreadingService.SwitchToUIThread();

        // get the id of some item (this "Start.js" item is from the standard sample)
        VsHierarchy.ParseCanonicalName("Start.js", out uint id);

        // get IVsUIShell from service provider
        VsHierarchy.GetSite(out Microsoft.VisualStudio.OLE.Interop.IServiceProvider sp);
        var shell = (IVsUIShell)sp.QueryService<IVsUIShell>();

        // get solution explorer's window
        var SolutionExplorer = new Guid(ToolWindowGuids80.SolutionExplorer);
        shell.FindToolWindow(0, ref SolutionExplorer, out IVsWindowFrame frame);

        // get solution explorer's DocView
        frame.GetProperty((int)__VSFPROPID.VSFPROPID_DocView, out object obj);
        var window = (IVsUIHierarchyWindow2)obj;

        // change attribute of the item
        window.SetItemAttribute((IVsUIHierarchy)VsHierarchy, id, (uint)__VSHIERITEMATTRIBUTE.VSHIERITEMATTRIBUTE_Bold, true);
    }
}

关于visual-studio - 在 VS 2017 自定义项目系统中,如何在解决方案资源管理器中将项目项设为粗体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45327220/

相关文章:

visual-studio - Visual Studio 2012 移动设备模拟器

angular - 如何使用 SpaTemplates 在 Visual Studio 2017 中向 ASP.NET Core Angular 2 应用程序添加很棒的字体

c++ - 如何避免与 native NuGet 包发生重复的 DLL 冲突?

asp.net-mvc - Visual Studio 2017 ASP.NET MVC核心模板中的Bower替换

react-native - 在 Visual Studio 2017 中 react native

c# - Visual Studio Package - 如何获取本地窗口中的信息?

c# - 从 IWpfTextView 获取非 cs 文件的文档路径

visual-studio-2013 - VS2013 不存在可扩展性项目类型

visual-studio - Visual Studio 2015 社区 + Resharper 9 : 2 pop-up lists show for intellisense

javascript - 如何使用 Visual Studio 2017 创建 NPM 包?