所以我已经有了这个上下文菜单,除了实际选择菜单项后面的事件似乎多次激活。我第一次点击它,它会发射一次,然后发射两次,然后发射3次。因此,在我刚给出的示例中,对于3次点击,它将总共发射6次(1 + 2 + 3)。那是为什么?
下面是关于我如何创建菜单项的代码。我把它剥掉了相关的部分;我省略了.Tag,.Visible和.Caption属性等内容。我正在使用.NET 3.5和VS 2008构建它。
提前致谢!!
Private WithEvents ActiveExplorerCBars As Office.CommandBars
Private app As New Outlook.Application
Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
ActiveExplorerCBars = app.ActiveExplorer.CommandBars
AddHandler ActiveExplorerCBars.OnUpdate, AddressOf ActiveExplorerCBars_OnUpdate
End Sub
//This seems to get hit A LOT
Private Sub ActiveExplorerCBars_OnUpdate()
Dim bar As Office.CommandBar
If IgnoreCommandbarsChanges Then Exit Sub
bar = ActiveExplorerCBars.Item("Context Menu")
If Not bar Is Nothing Then
Dim addMenu As Boolean = False
//this For loop just makes sure the context is only available when the user right-clicks over a mail item
For Each mail As Outlook.MailItem In Application.ActiveExplorer().Selection
addMenu = True
Exit For
Next
If addMenu Then
AddContextDropdown(bar)
End If
End If
End Sub
Private Sub AddContextDropdown(ByVal ContextMenu As Office.CommandBar)
Dim RootPopup As Office.CommandBarPopup
Dim popupTaskItem As Office.CommandBarPopup
RootPopup = ContextMenu.FindControl(Type:=Office.MsoControlType.msoControlPopup, Tag:="Update task")
If RootPopup Is Nothing Then
ChangingBar(ContextMenu, Restore:=False)
RootPopup = ContextMenu.Controls.Add(Type:=Office.MsoControlType.msoControlPopup)
Dim thisTaskPopup As Office.CommandBarPopup
popupTaskItem = ContextMenu.FindControl(Type:=Office.MsoControlType.msoControlPopup, Tag:=task.EntryID)
If popupTaskItem Is Nothing Then
popupTaskItem = RootPopup.Controls.Add(Type:=Office.MsoControlType.msoControlPopup)
thisTaskPopup = popupTaskItem
AddActionButtons(thisTaskPopup)
End If
End If
End Sub
Private Sub AddActionButtons(ByVal puItem As Office.CommandBarPopup)
Dim puDeploy As Office.CommandBarPopup = puItem.Controls.Add(Type:=Office.MsoControlType.msoControlPopup)
Dim btnActionItem As Office.CommandBarControl = puDeploy.Controls.Add(Type:=Office.MsoControlType.msoControlButton)
Dim thisButton As Office.CommandBarButton = btnActionItem
AddHandler thisButton.Click, AddressOf OnContextClick
End Sub
//Click event
Public Sub OnContextClick(ByVal ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean)
//This messagebox shows once the first time, twice the second, 3 times, etc
MessageBox.Show("Clicked: " & ctrl.Caption)
End Sub
答案 0 :(得分:1)
我明白了。 tobrien,我用你最后的评论作为一个工具来得出这个结论,特别是在你说:
可能是您实际创建了ADDITIONAL(具有相同签名的)回调
我用来添加处理程序的代码是:
AddHandler thisButton.Click, AddressOf OnContextClick
这怎么可以相同的签名?好吧,只有一个 OnContextClick 子...那么呢 thisButton 呢?
For Each value As ActivityType.Request In [Enum].GetValues(GetType(ActivityType.Request))
Dim btnActionItem As Office.CommandBarControl = puRequest.Controls.Add(Type:=Office.MsoControlType.msoControlButton)
With btnActionItem
.Tag = puRequest.Tag & "|" & value
.Caption = [Enum].GetName(GetType(ActivityType.Request), value)
.Visible = True
End With
Dim thisButton As Office.CommandBarButton = btnActionItem
AddHandler thisButton.Click, AddressOf OnContextClick
Next
此代码在OnUpdate发生时运行,如您所知,它始终发生。所以,实质上,每次OnUpdate命中时,我都会为EXACT SAME BUTTON添加一个额外的处理程序,而不是考虑每次OnUpdate发生时按钮基本上是新建的,并且其处理程序存储在内存中。
所以,我需要让按钮控件独一无二:
.Tag = puRequest.Tag & "|" & value & "|" & Now.ToBinary().ToString()
我刚刚在.Tag属性的末尾添加了一个Now.ToBinary()。ToString(),以确保每次为用户创建按钮时,它都有一个唯一的标记。现在事件是唯一的,每次点击只发射一次。
tobrien,我解决了你!虽然我最终回答了我自己的问题,但并非没有你的指导。谢谢!