VSX plans the Package file and vsx plans the package

Source: Internet
Author: User

VSX plans the Package file and vsx plans the package

VSX is a VS extension. It can be used to write plug-ins for different projects. Although there is not much time to get started with VSX, you can feel the charm of VSX after learning about VSX.

VSX has fewer materials and complicated configuration files. At the beginning, I had a lot of detours.

This article will help you better manage the Package file (the file that inherits the Package and registers the command). To understand this article, you should have a basic understanding of VSX, make sure that you can create a basic VSX menu. If you do not know about VSX, I recommend that you read my article 18 next year.

After reading the basic materials, I feel that the Package file contains a lot of content, including initialization, registration controls, menu filtering, and callback. Even if the menu filtering and callback are removed, there are also so many typical registration command codes:

1 OleMenuCommand cmdidFindInSolutionExplorer = new OleMenuCommand(FindInSolutionExplorerCallback,2                 new CommandID(GuidList.guidConvenientCmdSet, (int)PkgCmdIDList.cmdidFindInSolutionExplorer));3             cmdidFindInSolutionExplorer.BeforeQueryStatus += new EventHandler(MenuFilter.ClinicalFilter);4             mcs.AddCommand(cmdidFindInSolutionExplorer);

With the rich plug-in functions, more than a hundred commands make the Package file crowded, and with the addition of filtering and callback, every time you open this file, it becomes messy, (even if the echo and filter are separated into separate files, a large number of ECHO and filter functions are also ...).

The following solution allows you to use the registration command of the codemaid source code:

First, define an abstract class BaseCommand to inherit OleMenuCommand, and define two events under BaseCommand: OnBeforeQueryStatus and OnExecute, respectively, Corresponding filtering and callback:

 1         private static void BaseCommand_BeforeQueryStatus(object sender, EventArgs e) 2         { 3             BaseCommand command = sender as BaseCommand; 4             if (command != null) 5             { 6                 command.OnBeforeQueryStatus(); 7             } 8         } 9         protected virtual void OnBeforeQueryStatus()10         {11             Enabled = true;12         }13         private static void BaseCommand_Execute(object sender, EventArgs e)14         {15             BaseCommand command = sender as BaseCommand;16             if (command != null)17             {18                 command.OnExecute();19             }20         }21         protected virtual void OnExecute()22         {23         }

Register an event in the constructor:

1         protected ClinicalVSXPackage Pkg { get; private set; }2 3         protected BaseCommand(ClinicalVSXPackage pkg, CommandID id)4             : base(BaseCommand_Execute, id)5         {6             Pkg = pkg;7             BeforeQueryStatus += BaseCommand_BeforeQueryStatus;8         }

Pkg refers to the Package class itself to facilitate the transfer of members such as dte.

In this way, you can elegantly register the command in the Package file:

1 private readonly ICollection <BaseCommand> _ commands = new List <BaseCommand> (); 2 3 private void RegisterCommands () 4 {5 var mcs = MenuCommandService; 6 if (null = mcs) 7 return; 8 _ commands. add (new command 1 (this); 9 _ commands. add (new command 2 (this); 10 _ commands. add (new command 3 this); 11 .... 12 foreach (var command in _ commands) 13 mcs. addCommand (command); 14}

In this way, when you want to create a new command, you need to add an independent class to inherit the BaseCommand

And implements filtering and callback events. A typical command class:

1 internal class command 1: BaseCommand 2 {3 public command 1 (Package pkg) 4: base (pkg, new CommandID (eclipsetid, (int) PkgCmdIDList. command ID) 5 {} 6 7 protected override void OnBeforeQueryStatus () 8 {9} 10 protected override void OnExecute () 11 {12 base. onExecute (); 13} 14}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.