简体   繁体   中英

How to enable a new custom Context Menu Item in Visual Studio Solution Explorer?

I would like to have "File.CopyRelativePath" as an option in the Context Menu of Solution Explorer when I right click a file.

I am using Visual Studio 2022

I have managed to put the command in the menu by adding it at: Tools - Customize - Commands - Context menu - Project and Solution Context Menus | Item

在此处输入图像描述

There I have added File.CopyRelativePath and given it a shortcut too.

在此处输入图像描述

But when I see it in the context menu it is disabled.

我的新自定义上下文菜单项被禁用:(

How do I enable the menu item so I can start using it?

TL;DR: You can't force the command handler to be enabled. However, you could implement your own handler for this command (or a brand new command with the same label).

More details:

There's a lot of documentation about how commands work in VS, but to quickly paraphrase: command handlers implement an Execute method to do the operation and optionally a QueryStatus method to determine if the command is currently able to execute. In this case, QueryStatus is determining that the command should not be enabled.

The implementation of this command's QueryStatus (I peeked) looks at the currently active WindowFrame, and requires that it be associated with an open document. Once the focus is in Solution Explorer, it would determine that the command should not be enabled as Solution Explorer is not a document window.

(The implementation, similarly, uses properties of the frame that are only set for a document instance. It wouldn't understand an input from another type of window.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM