简体   繁体   English

Visual Studio包:设置自定义解决方案资源管理器上下文菜单项的可见性

[英]Visual Studio Package: Settings the visibility of a custom Solution Explorer context menu item

I am creating a Visual Studio Package (this is my first time) and my end goal is to create a context-menu item for the solution explorer that only works on certain file types. 我正在创建一个Visual Studio包(这是我的第一次),我的最终目标是为解决方案资源管理器创建一个仅适用于某些文件类型的上下文菜单项。 (I thought this would be a common thing, but didn't find any decent tutorials on it, so if you know any please let me know) (我认为这将是一个常见的事情,但没有找到任何体面的教程,所以如果你知道任何请告诉我)

I followed a simple MSDN guide to create an item in the toolbar first (I forget where is was to link it) and this worked fine. 我按照一个简单的MSDN指南首先在工具栏中创建一个项目(我忘记链接它的位置),这很好。

Then I found a way to move it to the Solution Explorer context menu. 然后我找到了将其移动到Solution Explorer上下文菜单的方法。 This was achieved by manipulating the .vsct file and having an element like this: 这是通过操纵.vsct文件并具有如下元素来实现的:

<Parent guid="guidSHLMainMenu" id="IDM_VS_CTXT_ITEMNODE"/>

That probably doesn't matter, but I am trying to set the scene. 这可能没关系,但我试图设置场景。

Now, because I want to only show the item for certain file types, I need to find a way to check the file when the right-click button is pressed. 现在,因为我只想显示某些文件类型的项目,所以我需要找到一种方法来在按下右键单击按钮时检查文件。 Cutting a long search short, I found this and ended up with the following code: 缩短搜索时间,我找到了这个并最终得到以下代码:

protected override void Initialize()
{
    //stuff
    OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
    menuItem.BeforeQueryStatus += menuItem_BeforeQueryStatus;
    //more stuff
}

void menuItem_BeforeQueryStatus(object sender, EventArgs e)
{
    var myCommand = sender as OleMenuCommand;
    myCommand.Text = "NEW NAME";
}

In the above example I am just trying to set the text to try and prove it works, I know there is a Visible property but I want this step to work first. 在上面的例子中,我只是试图设置文本来尝试并证明它有效,我知道有一个Visible属性,但我希望这一步先工作。 The BeforeQueryStatus event is fired, and debugging shows the code executing as expected. 触发BeforeQueryStatus事件,并且调试显示代码按预期执行。 However, there is no change in the context menu item, it stays with the original text. 但是,上下文菜单项没有变化,它保留原始文本。

What am I missing here? 我在这里错过了什么? Why is it not updating? 为什么不更新?

OK, so I have finally found a solution to this problem, there are a couple of things that you need to do... 好的,所以我终于找到了解决这个问题的方法,你需要做几件事......

STEP 1 : 第1步

We need to specify that the VSPackage should "auto-load", we do this so that the code will execute when the ContextMenu is shown, because normally the VSPackage would not initialise before the UI has been shown (ie the menu item has been clicked). 我们需要指定VSPackage应该“自动加载”,我们这样做是为了在显示ContextMenu时执行代码,因为通常VSPackage在UI显示之前不会初始化(即菜单项已被点击)。 To do this we add an attribute to the Package class, like so: 为此,我们向Package类添加一个属性,如下所示:

[ProvideAutoLoad("{f1536ef8-92ec-443c-9ed7-fdadf150da82}")]
public sealed class MyFirstPackage : Package

You may wonder what that GUID value is, well in this case it represents the UICONTEXT_SolutionExists constant, which means the the package will auto-load when a solution exists (so when we create a new one or load one). 您可能想知道GUID值是什么,在这种情况下它代表UICONTEXT_SolutionExists常量,这意味着当解决方案存在时,包将自动加载(因此当我们创建一个新的或加载一个时)。 I got this information from here , as you might be able to tell there are a number of different VSConstants that could be used. 我从这里得到了这些信息 ,因为你可能会知道有许多不同的VSConstants可以使用。

Here are a couple more resources that list other GUID values that can be used: 以下是一些列出可以使用的其他GUID值的资源:

STEP 2 : 第2步

Now that the BeforeQueryStatus code is executing at the correct place, it is still confusing as to why the code doesn't actually change anything (in my question I try to change the Text ). 既然BeforeQueryStatus代码在正确的位置执行,那么为什么代码实际上没有改变任何东西仍然令人困惑(在我的问题中我尝试更改Text )。 Well, the answer is, because we need to give the package permission to do so (at least that's the way I see it as being). 嗯,答案是,因为我们需要授予包的权限(至少这是我认为的方式)。

To do this we must edit the .vsct file. 为此,我们必须编辑.vsct文件。 Inside there we can find a Buttons element, inside which should be our ContextMenu Button . 在里面我们可以找到一个Buttons元素,里面应该是我们的ContextMenu Button By default there are some comments which mention the use of the CommandFlag node - this is what we want. 默认情况下,有一些注释提到了CommandFlag节点的使用 - 这就是我们想要的。

In order to give permission for our package to change the Text we must add the following node: 为了允许我们的包更改Text我们必须添加以下节点:

<CommandFlag>TextChanges</CommandFlag>

Now, if we run the VSPackage it should all work as expected! 现在,如果我们运行VSPackage,它应该按预期工作!

If you are looking to allow permission to change the Visibility of the menu item (which was my original aim) then you can use the following CommandFlag : 如果您希望允许更改菜单项的Visibility (这是我最初的目标),那么您可以使用以下CommandFlag

<CommandFlag>DynamicVisibility</CommandFlag>

There is a full list of command flags here , with descriptions on what they do. 这里有一个完整的命令标志列表 ,并描述了它们的作用。

Instead of directly using the guid mentioned in musefan's answer, you can use: 而不是直接使用musefan的答案中提到的guid,你可以使用:

    [ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids.SolutionExists)]

Refer to: UIContextGuids Class for all guid constants. 请参阅:所有guid常量的UIContextGuids类

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio加载项-将上下文菜单项添加到solution-explorer - Visual Studio Add-In - adding a context menu item to solution-explorer Visual Studio AddIn:如何将项目特定命令添加到解决方案资源管理器上下文菜单 - Visual Studio AddIn: How do I Add Item Specific Commands to the Solution Explorer Context Menu 在Visual Studio解决方案资源管理器中将菜单项添加到.cs文件(仅)? - Add menu item to .cs files (only) in Visual Studio solution explorer? Visual Studio 2010插件 - 将上下文菜单添加到解决方案资源管理器中 - Visual Studio 2010 Plug-in - Adding a context-menu to the Solution Explorer 我的上下文菜单项由Visual Studio软件包中的菜单命令完成,未显示在cshtml文件中 - My context menu item made by menu command in Visual Studio Package is not shown in cshtml files Visual Studio 2008:通过项目/解决方案上下文菜单启动自定义构建目标 - 无需加载项 - Visual Studio 2008: Start custom build target via project/solution context menu - without add-in 将自定义项模板添加到 Visual Studio 解决方案 - Add custom Item Template to Visual Studio Solution Visual Studio Shell 2015不在解决方案资源管理器中使用自定义图标 - Visual Studio Shell 2015 does not use custom icons in solution explorer Visual Studio 解决方案资源管理器未在解决方案中显示项目 - Visual Studio Solution Explorer not showing projects in solution 自定义 Visual Studio 解决方案 - Custom Visual Studio solution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM