如何:向 SharePoint 项目项扩展中添加快捷菜单项
可以通过使用项目项扩展向现有 SharePoint 项目项中添加快捷菜单项。 当用户在**“解决方案资源管理器”**中右击某个项目项时,将会显示相应的菜单项。
下面的步骤假定您已创建了一个项目项扩展。 有关更多信息,请参见如何:创建 SharePoint 项目项扩展。
在项目项扩展中添加快捷菜单项
在 ISharePointProjectItemTypeExtension 实现的 Initialize 方法中,处理 projectItemType 参数的 ProjectItemMenuItemsRequested 事件。
在 ProjectItemMenuItemsRequested 事件的事件处理程序中,将新的 IMenuItem 对象添加到事件实参参数的 ViewMenuItems 或 AddMenuItems 集合中。
示例
下面的代码示例演示如何将快捷菜单项添加到事件接收器项目项中。 当用户在**“解决方案资源管理器”中右击该项目项并单击“Write Message to Output Window”(向输出窗口写入消息)菜单项时,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 工具扩展。