HOW TO:將捷徑功能表項目加入至 SharePoint 專案項目擴充功能
您可以使用專案項目擴充功能,將捷徑功能表項目加入至現有的 SharePoint 專案項目。使用者在 [方案總管] 中以滑鼠右鍵按一下專案項目時,功能表項目就會出現。
下列步驟假設您已經建立專案項目擴充功能。如需詳細資訊,請參閱 HOW TO:建立 SharePoint 專案項目擴充功能。
若要在專案項目擴充功中加入捷徑功能表項目
在您的 ISharePointProjectItemTypeExtension 實作 Initialize 方法中,請處理 projectItemType 參數的 ProjectItemMenuItemsRequested 事件。
在 ProjectItemMenuItemsRequested 事件的事件處理常式中,將新的 IMenuItem 物件加入至事件引數參數的 ViewMenuItems 或 AddMenuItems 集合。
範例
下列程式碼範例示範如何將捷徑功能表項目加入至 [事件接收器] 專案項目。當使用者以滑鼠右鍵按一下 [方案總管] 中的專案項目,並且按一下 [將訊息寫入輸出視窗] 功能表項目時,Visual Studio 會在 [輸出] 視窗中顯示訊息。
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 專案服務來將訊息寫入 [輸出] 視窗。如需詳細資訊,請參閱 使用 SharePoint 專案服務。
編譯程式碼
這個範例需要類別庫專案並參考下列組件:
Microsoft.VisualStudio.SharePoint
System.ComponentModel.Composition
部署擴充功能
若要部署擴充功能,請針對組件以及要與擴充功能一起散發的任何其他檔案建立 Visual Studio 擴充功能 (VSIX) 套件。如需詳細資訊,請參閱部署 Visual Studio 中 SharePoint 工具的擴充功能。