擴充 Visual Studio for Mac 逐步解說Extending Visual Studio for Mac Walkthrough

本主題會引導您建立簡單的延伸模組套件This topic guides you through building a simple extension package. 延伸模組套件將在 Visual Studio for Mac 的 [編輯] 功能表中建立新的命令,讓使用者可在開啟的文字文件中插入目前日期和時間。The extension package will create a new Command in Visual Studio for Mac's Edit menu that allows the user to insert the current date and time into an open text document.

此範例使用增益集製作程式。This example uses the Add-in Maker. 增益集製作程式會建立新的專案範本,並填入我們自訂延伸模組套件所需的檔案。The Add-In Maker creates a new Project template and populates it with the required files for our custom extension package.

  1. 如果尚未開啟,請先啟動 Visual Studio for Mac:Begin by launching Visual Studio for Mac if it's not already open:

    Visual Studio for Mac 螢幕擷取畫面

  2. 使用延伸模組管理員安裝 增益集製作程式延伸模組套件Install the Add-in Maker extension package using the Extension Manager. 從 Visual Studio 功能表,選擇 [延伸模組]:From the Visual Studio menu, choose Extensions...:

    [增益集管理員] 索引標籤

  3. 巡覽至 [資源庫] 索引標籤,然後在右上搜尋列鍵入 Addin MakerNavigate to the Gallery tab and type Addin Maker into the top-right search bar. 從 [增益集開發] 類別選取 [增益集製作程式],然後按一下 [安裝]Select Addin Maker from the Add-in Development category and click Install. 如果未顯示任何內容,請點擊 [重新整理],並再次搜尋:If nothing shows up, hit Refresh and search again:

    增益集管理員

  4. 現在已安裝增益集製作程式,您可以開始建置延伸模組套件。Now that the Addin Maker is installed, you can start building an extension package. 開始建立新的解決方案。Start by creating a new solution.

  5. 從 [新增解決方案] 對話方塊,選擇 [其他] > [其他] > [一般] > [Xamarin Studio 增益集] > [C#] 範本,並在接下來的螢幕為新的解決方案命令為 DateInserterFrom the New Solution dialog, choose Other > Miscellaneous > General > Xamarin Studio Addin > C# template and on the following screen name the new Solution DateInserter:

    建立新的解決方案

  6. Visual Studio for Mac 將會填入新的解決方案:Visual Studio for Mac will populate a new Solution:

    填入的解決方案

  7. 移除 Manifest.addin.xml 中的範本程式碼,並取代為下列內容:Remove the template code in Manifest.addin.xml and replace it with the following:

    <?xml version="1.0" encoding="UTF-8"?>
       <ExtensionModel>
           <Extension path = "/MonoDevelop/Ide/Commands/Edit">
               <Command id = "DateInserter.DateInserterCommands.InsertDate"
                   _label = "Insert Date"
                   defaultHandler = "DateInserter.InsertDateHandler" />
           </Extension>
    
           <Extension path = "/MonoDevelop/Ide/MainMenu/Edit">
               <CommandItem id="DateInserter.DateInserterCommands.InsertDate" />
           </Extension>
       </ExtensionModel>
    
  8. 現在您需要設定最終將處理在文字編輯器中插入日期和時間的檔案。Now you need to set up the files that will eventually handle inserting the date and time into the text editor. 以滑鼠右鍵按一下專案節點,然後新增檔案。Right-Click on the project node and add a new file. 選取 [一般] > [空類別] 並將新檔案命名為 InsertDateHandlerSelect General > Empty Class and name the new file InsertDateHandler:

    插入日期處理常式

  9. 讓我們移除 InsertDateHandler.cs 中的範本程式碼,並取代為下列程式碼:Let's remove the template code from InsertDateHandler.cs and replace it with the following code:

    using MonoDevelop.Components.Commands;
    using MonoDevelop.Ide;
    using MonoDevelop.Ide.Gui;
    using System;
    
    namespace DateInserter
    {
       class InsertDateHandler : CommandHandler
       {
           protected override void Run()
           {
    
           }
    
           protected override void Update(CommandInfo info)
           {
    
           }
       }
    }
    

    我們稍後會展開這兩個預留位置方法。We'll expand these two placeholder methods later.

  10. 以滑鼠右鍵按一下 DateInserter 專案並選取 [新增] > [新增檔案]。Right-click on the DateInserter Project and select Add > New File. 選取 [一般] > [列舉是空的],然後將新檔案命名為 DateInserterCommandsSelect General > Empty Enumeration, and then name the new file DateInserterCommands:

    DateInserterCommands

  11. 新增 InsertDate 命令作為 DateInserterCommands.cs 檔案中的新列舉:Add the InsertDate Command as a new enumeration in the DateInserterCommands.cs file:

    using System;
    
    namespace DateInserter
    {
      public enum DateInserterCommands
      {
          InsertDate,
      }
    }
    
  12. 此時,您應該有能使用的延伸模組套件。At this point, you should have a working extension package. 您可以儲存工作並執行應用程式來測試它。You can test it out by saving your work and running the application. IDE 就會啟動 Visual Studio for Mac 的新執行個體,並安裝新的延伸模組套件。The IDE will launch a new instance of Visual Studio for Mac with the new extension package installed. 如果您巡覽至 [編輯] 功能表,您會看到 Visual Studio for Mac 有新的選項,稱為 插入日期,如以下螢幕擷取畫面所示:If you navigate to the Edit menu, you'll see that Visual Studio for Mac has a new option called Insert Date, as illustrated by the screenshot below:

    插入日期命令

    請注意,從功能表中選取 [插入日期] 不會有任何效果,因為目前的實作只有預留位置方法。Note that selecting Insert Date from the menu has no effect as the current implementation only has placeholder methods.

  13. 架構已針對延伸模組套件準備就緒,可以撰寫程式碼啟用插入日期。The framework is in place for the extension package, and it's time to write the code that powers inserting the date. 首先,請確定 [插入日期命令] 只有在使用者開啟文字檔案時啟用,方法是將 InsertDateHandler.cs 中的 Update 方法取代為下列程式碼:First, make sure that the Insert Date Command is only enabled when the user has a text file open by replacing the Update method in InsertDateHandler.cs with the following code:

    protected override void Update(CommandInfo info)
    {
      info.Enabled = IdeApp.Workbench.ActiveDocument?.Editor != null;
    }
    
  14. 以下列程式碼更新命令的 Run 方法,插入日期和時間:Update the Command's Run method to insert the date and time with the following code:

    protected override void Run () {
      var editor = IdeApp.Workbench.ActiveDocument.Editor;
      var date = DateTime.Now.ToString ();
      editor.InsertAtCaret (date);
    
    }
    
  15. 最後,讓我們執行延伸模組套件來進行測試。Finally, let's run our extension package to test it. 在 Visual Studio for Mac 的新執行個體中,選取 [編輯] > [插入日期]。In the new instance of Visual Studio for Mac, select Edit > Insert Date. 目前的日期和時間會插入在我們的插入號,如以下螢幕擷取畫面所示:The current date and time is inserted at our caret, as illustrated by the screenshot below:

    插入日期螢幕擷取畫面

請參閱See also