如何:將快捷方式功能表項目加入至 SharePoint 專案專案延伸模組How to: Add a shortcut menu item to a SharePoint project item extension

您可以使用專案專案延伸,將快捷方式功能表項目加入至現有的 SharePoint 專案專案。You can add a shortcut menu item to an existing SharePoint project item by using a project item extension. 當使用者以滑鼠右鍵按一下 方案總管 中的專案專案時,就會顯示功能表項目。The menu item appears when a user right-clicks the project item in Solution Explorer.

下列步驟假設您已建立專案專案延伸模組。The following steps assume that you have already created a project item extension. 如需詳細資訊,請參閱 如何:建立 SharePoint 專案專案延伸模組。For more information, see How to: Create a SharePoint project item extension.

在專案專案延伸中加入快捷方式功能表項目To add a shortcut menu item in a project item extension

  1. Initialize 您的實作為方法中 ISharePointProjectItemTypeExtension ,處理 ProjectItemMenuItemsRequested projectItemType 參數的事件。In the Initialize method of your ISharePointProjectItemTypeExtension implementation, handle the ProjectItemMenuItemsRequested event of the projectItemType parameter.

  2. 在事件的事件處理常式中 ProjectItemMenuItemsRequested ,將新的 IMenuItem 物件加入至 ViewMenuItems AddMenuItems 事件引數參數的或集合。In your event handler for the ProjectItemMenuItemsRequested event, add a new IMenuItem object to the ViewMenuItems or AddMenuItems collection of the event arguments parameter.

  3. Click 新物件的事件處理常式中 IMenuItem ,執行您要在使用者按一下快捷方式功能表項目時執行的工作。In the Click event handler for the new IMenuItem object, perform the tasks you want to execute when a user clicks your shortcut menu item.

範例Example

下列程式碼範例示範如何將快捷方式功能表項目加入至事件接收器專案專案。The following code example demonstrates how to add a shortcut menu item to the Event Receiver project item. 當使用者以滑鼠右鍵按一下 方案總管 中的專案專案,然後按一下 [ 寫入訊息] 輸出視窗 功能表項目時,Visual Studio 會在 [ 輸出 ] 視窗中顯示一則訊息。When the user right-clicks the project item in Solution Explorer and clicks the Write Message to Output Window menu item, Visual Studio displays a message in the Output window.

Imports System
Imports System.ComponentModel.Composition
Imports Microsoft.VisualStudio.SharePoint

Namespace Contoso.Examples.ProjectItemExtensionWithMenu

    <Export(GetType(ISharePointProjectItemTypeExtension))> _
    <SharePointProjectItemType("Microsoft.VisualStudio.SharePoint.EventHandler")> _
    Friend Class ExampleProjectItemExtensionWithMenu
        Implements ISharePointProjectItemTypeExtension

        Private Sub Initialize(ByVal projectItemType As ISharePointProjectItemType) _
            Implements ISharePointProjectItemTypeExtension.Initialize
            AddHandler projectItemType.ProjectItemMenuItemsRequested, AddressOf ProjectItemMenuItemsRequested
        End Sub

        Private Sub ProjectItemMenuItemsRequested(ByVal Sender As Object,
            ByVal e As SharePointProjectItemMenuItemsRequestedEventArgs)
            Dim menuItem As IMenuItem = e.ViewMenuItems.Add("Write Message to Output Window")
            AddHandler menuItem.Click, AddressOf MenuItem_Click
        End Sub

        Private Sub MenuItem_Click(ByVal Sender As Object, ByVal e As MenuItemEventArgs)
            Dim projectItem As ISharePointProjectItem = CType(e.Owner, ISharePointProjectItem)
            projectItem.Project.ProjectService.Logger.WriteLine(
                String.Format("This message was written from a shortcut menu for {0}.", projectItem.Name),
                LogCategory.Status)
        End Sub
    End Class
End Namespace
using System;
using System.ComponentModel.Composition;
using Microsoft.VisualStudio.SharePoint;

namespace Contoso.Examples.ProjectItemExtensionWithMenu
{
    [Export(typeof(ISharePointProjectItemTypeExtension))]
    [SharePointProjectItemType("Microsoft.VisualStudio.SharePoint.EventHandler")]
    internal class ExampleProjectItemExtensionWithMenu : ISharePointProjectItemTypeExtension
    {
        public void Initialize(ISharePointProjectItemType projectItemType)
        {
            projectItemType.ProjectItemMenuItemsRequested += projectItemType_ProjectItemMenuItemsRequested;
        }

        void projectItemType_ProjectItemMenuItemsRequested(object sender, 
            SharePointProjectItemMenuItemsRequestedEventArgs e)
        {
            IMenuItem menuItem = e.ViewMenuItems.Add("Write Message to Output Window");
            menuItem.Click += MenuItemExtension_Click;
        }

        void MenuItemExtension_Click(object sender, MenuItemEventArgs e)
        {
            ISharePointProjectItem projectItem = (ISharePointProjectItem)e.Owner;
            projectItem.Project.ProjectService.Logger.WriteLine(
                String.Format("This message was written from a shortcut menu for {0}.", projectItem.Name), 
                LogCategory.Status);
        }
    }
}

這個範例會使用 SharePoint 專案服務,將訊息寫入至 [ 輸出 ] 視窗。This example uses the SharePoint project service to write the message to the Output window. 如需詳細資訊,請參閱 使用 SharePoint 專案服務For more information, see Use the SharePoint project service.

編譯程式碼Compile the code

此範例需要類別庫專案,其中包含下列元件的參考:This example requires a class library project with references to the following assemblies:

  • VisualStudio SharePointMicrosoft.VisualStudio.SharePoint

  • System.ComponentModel.CompositionSystem.ComponentModel.Composition

部署延伸模組Deploy the extension

若要部署擴充功能,請 Visual StudioVisual Studio 為元件和您想要使用擴充功能散發的任何其他檔案,建立 (VSIX) 封裝的延伸模組。To deploy the extension, create a Visual StudioVisual Studio extension (VSIX) package for the assembly and any other files that you want to distribute with the extension. 如需詳細資訊,請參閱 Visual Studio 中的部署 SharePoint 工具的擴充功能。For more information, see Deploy extensions for the SharePoint tools in Visual Studio.

請參閱See also