禁用Visual Studio加载项选项

本文关键字:选项 加载项 Studio Visual 禁用 | 更新日期: 2023-09-27 18:20:47

我正在开发Visual studio插件。我正在Connect.cs类的OnConnection()方法中填充visual studio加载项选项。

现在,我想基于打开的host项目禁用外接程序选项。

例如,如果web project处于打开状态,我希望启用附加模块选项。否则应禁用它。

connect.cs类中的哪一个event可以实现这一点,以及如何实现?

禁用Visual Studio加载项选项

这应该可以做到:

    _applicationObject.Events.SolutionEvents.Opened += new _dispSolutionEvents_OpenedEventHandler(openedSolution);
    _applicationObject.Events.SolutionEvents.AfterClosing += new _dispSolutionEvents_AfterClosingEventHandler(closedSolution);

MSDN中的"内部"参考:http://msdn.microsoft.com/de-de/library/EnvDTE.aspx

您可以使用此代码(从http://www.mztools.com/articles/2007/mz2007016.aspx):

public string GetProjectTypeGuids(EnvDTE.Project proj)
    {
        string projectTypeGuids = "";
        object service = null;
        Microsoft.VisualStudio.Shell.Interop.IVsSolution solution = null;
        Microsoft.VisualStudio.Shell.Interop.IVsHierarchy hierarchy = null;
        Microsoft.VisualStudio.Shell.Interop.IVsAggregatableProject aggregatableProject = null;
        int result = 0;
        service = GetService(proj.DTE, typeof(Microsoft.VisualStudio.Shell.Interop.IVsSolution));
        solution = (Microsoft.VisualStudio.Shell.Interop.IVsSolution)service;
        result = solution.GetProjectOfUniqueName(proj.UniqueName, hierarchy);
        if (result == 0)
        {
            aggregatableProject = (Microsoft.VisualStudio.Shell.Interop.IVsAggregatableProject)hierarchy;
            result = aggregatableProject.GetAggregateProjectTypeGuids(projectTypeGuids);
        }
        return projectTypeGuids;
    }
    public object GetService(object serviceProvider, System.Type type)
    {
        return GetService(serviceProvider, type.GUID);
    }
    public object GetService(object serviceProviderObject, System.Guid guid)
    {
        object service = null;
        Microsoft.VisualStudio.OLE.Interop.IServiceProvider serviceProvider = null;
        IntPtr serviceIntPtr;
        int hr = 0;
        Guid SIDGuid;
        Guid IIDGuid;
        SIDGuid = guid;
        IIDGuid = SIDGuid;
        serviceProvider = (Microsoft.VisualStudio.OLE.Interop.IServiceProvider)serviceProviderObject;
        hr = serviceProvider.QueryService(SIDGuid, IIDGuid, serviceIntPtr);
        if (hr != 0)
        {
            System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(hr);
        }
        else if (!serviceIntPtr.Equals(IntPtr.Zero))
        {
            service = System.Runtime.InteropServices.Marshal.GetObjectForIUnknown(serviceIntPtr);
            System.Runtime.InteropServices.Marshal.Release(serviceIntPtr);
        }
        return service;
    }

你可以在这里找到已知GUID的列表

要禁用您的选项,您需要删除或添加关于openedSolution方法中的类型(检查GUID)的菜单项