简体   繁体   中英

Visual Studio 2022 view code button missing on solution explorer

I'm doing a WinForms app (which I haven't done in a long time) and noticed that in VS 2022, the "view code" button (looks like <> ) which used to be on the toolbar on the "Solution Explorer" window is missing. I know I can right-click on a form and reach it through the context menu, or hit F7 , but is there any way to add it back? I can't seem to find any option for it.

We can see “View Code” button in a solution which contain a .NET Framework winform project.

在此处输入图像描述

If we create a solution which contains a .NET 6 winform project the “View Code” button will disappear. We should right-click and reach it through the context menu or hit F7.

在此处输入图像描述

This issue has been submitted and is waiting to be resolved. https://developercommunity.visualstudio.com/t/Solution-Explorers-tool-bar-does-not-in/1528389?space=8&q=vs+2022+Missing+%3C%3E+%22View+Code%22+option+in+Solution+Explorer&stateGroup=active

I'm not aware of any settings for that, it might be by design or might be a missing feature.

Meanwhile, as a workaround you may want to create a VS extension that adds the command to Solution explorer, then set the visibility rule for.cs files. Then in the action of the command, find the selected item and open it with code editor.

Add a button to solution explorer to open.cs files in code editor

This extension adds a toolbar button to the Solution Explore which will be visible, only if you choose a C# file, and when you click on it, it opens the file in code editor:

在此处输入图像描述

You can download or clone the solution here:

Here is the step by step guide on creating this extension:

  1. Add new project and choose ' VSIX Project ' and leave the default name.
    Please note: To have this project type, you need to install ' Visual Studio extension development ' when installing/modifying VS installation.

  2. Add new item and choose ' Command ' which is under ' Extensibility ' group.

  3. Open 'VSIXProject1Package.cs' file and add the following line of code to the class:

     public const string UIContextGuid = "8B40D5E2-5626-42AE-99EF-3DD1EFF46E7B";

    It could be any GUID.

  4. Open 'VSIXProject1Package.vsct' file and add the following as child of <Symbols> node:

     <GuidSymbol name="UIContextGuid" value="{8B40D5E2-5626-42AE-99EF-3DD1EFF46E7B}" />

    The GUID here should be same as the GUID in previous step.

  5. Modify the attributes of the '' class to be like this:

     [PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)] [Guid(VSIXProject1Package.PackageGuidString)] [ProvideMenuResource("Menus.ctmenu", 1)] [ProvideUIContextRule(VSIXProject1Package.UIContextGuid, name: "Supported Files", expression: "CSharp", termNames: new[] { "CSharp" }, termValues: new[] { "HierSingleSelectionName:.cs$" })] public sealed class VSIXProject1Package: AsyncPackage
  6. Open 'Command1.cs' and modify the ServiceProvider property to this:

     private IServiceProvider ServiceProvider { get { return this.package; } }
  7. Replace the ExecuteCommand with this:

     private void Execute(object sender, EventArgs e) { ThreadHelper.ThrowIfNotOnUIThread(); var dte = ServiceProvider.GetService(typeof(DTE)) as DTE; ProjectItem item = dte.SelectedItems.Item(1)?.ProjectItem; if (item.= null) { VsShellUtilities,OpenDocumentWithSpecificEditor(package. item,FileNames[1]. Microsoft.VisualStudio.VSConstants.VsEditorFactoryGuid,TextEditor_guid. Microsoft.VisualStudio.VSConstants.LOGVIEWID;Code_guid); } }
  8. Add the following method:

     private void MyQueryStatus(object sender, EventArgs e) { ThreadHelper.ThrowIfNotOnUIThread(); var button = (MenuCommand)sender; button.Visible = false; var dte = ServiceProvider.GetService(typeof(DTE)) as DTE; ProjectItem item = dte.SelectedItems.Item(1)?.ProjectItem; if (item.= null) { string fileExtension = Path.GetExtension(item.Name);ToLowerInvariant(). string[] supportedFiles = new[] { ";cs"}. button.Visible = supportedFiles;Contains(fileExtension); } }
  9. Open the '' file and replace the content of the Groups node with the following:

     <Group guid="guidVSIXProject1PackageCmdSet" id="SolutionToolbarGroup" priority="0xF000"> <Parent guid="guidSHLMainMenu" id="IDM_VS_TOOL_PROJWIN"/> </Group>
  10. Replace the child of the GuidSymbol which has name = guidVSIXProject1PackageCmdSet with the following:

     <IDSymbol name="Command1Id" value="0x0100" /> <IDSymbol name="SolutionToolbarGroup" value="0x0190"/>
  11. Add the visibility constraint, right after the end of </Commands> tag:

     <VisibilityConstraints> <VisibilityItem guid="guidVSIXProject1PackageCmdSet" id="Command1Id" context="UIContextGuid" /> </VisibilityConstraints>
  12. Modify the generated button tag to:

     <Button guid="guidVSIXProject1PackageCmdSet" id="Command1Id" priority="0x0100" type="Button"> <Parent guid="guidVSIXProject1PackageCmdSet" id="SolutionToolbarGroup" /> <Icon guid="guidImages" id="bmpPic1" /> <CommandFlag>DefaultInvisible</CommandFlag> <CommandFlag>DynamicVisibility</CommandFlag> <Strings> <ButtonText>Invoke Command1</ButtonText> </Strings> </Button>
  13. Run the project and see the result.

More information:

This is a far from perfect workaround . But:

You can add the button to your normal toolbar:

Right click the toolbar area, 'Customize...', 'Commands', 'Toolbar', select 'Text editor' (or what you want), 'Add command...', 'View', 'View code' and 'Ok'.

Now you have the button on your toolbar.

However, it's far from perfect, because you must have the Form/razor page open in the editor, or the button will be disabled.

I don't think Microsoft will ever add the button in Solution Explorer again - it's after all removed on purpose, but 'mouse men' like us surely miss it.

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