我想尝试使用新的System.Reactive技术来简化在ListView中对上下文菜单点击响应执行操作的过程。
到目前为止,所有设置部分似乎都更容易了,但我正在努力以正确的方式组合两个事件流(项目选择和菜单点击)。
以下是我目前的代码:
(bookListView包含我的书籍,并显示菜单bookListContextMenu)
private void frmBookList_Load(object sender, EventArgs e)
{
//filter click events to right clicks over a ListViewItem containing a actual book
var rightclicks = from click in Observable.FromEventPattern<MouseEventArgs>(bookListView, "MouseClick")
where click.EventArgs.Button == MouseButtons.Right &&
ClickedOnBook((ListView)click.Sender, click.EventArgs) != null
select click;
//subscribe to clicks to display context menu at clicked location
rightclicks.Subscribe(click => bookListContextMenu.Show((Control)click.Sender, click.EventArgs.Location));
//subscribe to clicks again to convert click into clicked book
var rightclickedbook = rightclicks.Select(click => ClickedOnBook((ListView)click.Sender, click.EventArgs));
//capture context menu click, convert to action enum
var clickaction = Observable.FromEventPattern<ToolStripItemClickedEventArgs>(bookListContextMenu, "ItemClicked")
.Select(click => GetAction(click.EventArgs.ClickedItem));
//combine the two event streams to get a pair of Book and Action
//can project to an anonymoue type as it will be consumed within this method
var bookaction = clickaction.CombineLatest(rightclickedbook, (action, book) => new { Action = action, Book = book });
//subscribe to action and branch to action specific method
bookaction.Subscribe(doaction =>
{
switch (doaction.Action)
{
case BookAction.Delete:
DeleteBookCommand(doaction.Book);
break;
case BookAction.Edit:
EditBookCommand(doaction.Book);
break;
}
});
}
public enum BookAction
{
None = 0,
Edit = 1,
Delete = 2
}
private BookAction GetAction(ToolStripItem item)
{
if (item == deleteBookToolStripMenuItem) return BookAction.Delete;
else if (item == editBookToolStripMenuItem) return BookAction.Edit;
else return BookAction.None;
}
private Book ClickedOnBook(ListView lview, MouseEventArgs click)
{
ListViewItem lvitem = lview.GetItemAt(click.X, click.Y);
return lvitem != null ? lvitem.Tag as Book : null;
}
private void DeleteBookCommand(Book selectedbook)
{
//code to delete a book
}
private void EditBookCommand(Book selectedbook)
{
//code to edit a book
}
问题在于合并函数。如果我使用'CombineLatest',那么在第一次使用上下文菜单后,每次右键单击都会在新选择上再次调用先前的操作。
如果我使用'Zip',然后右键单击一本书,但是点击了上下文菜单而不是它,那么下一次右键单击并实际点击菜单时,动作将被调用在第一个选择上而不是第二个选择上。
我尝试了各种形式的时间限制缓冲区和窗口以及最新等等,但通常只能在菜单出现时立即阻止,以至于无法选择任何项,或者如果显示了菜单但没有单击任何项,则会收到有关空序列的异常。
我相信肯定有一种更简单的方法来做到这一点,但我不确定是什么。
也许是这个?
//the menu may be closed with or without clicking any item
var contextMenuClosed = Observable.FromEventPattern(bookListContextMenu, "Closed");
var contextMenuClicked = Observable.FromEventPattern<ToolStripItemClickedEventArgs>(bookListContextMenu, "ItemClicked");
//combine the two event streams to get a pair of Book and Action
//which we can project to an anonymoue type as it will be consumed within this method
var bookaction = from mouseclick in rightclicks
let book = ClickedOnBook((ListView)mouseclick.Sender, mouseclick.EventArgs)
from menuclick in contextMenuClicked.Take(1).TakeUntil(contextMenuClosed)
let action = GetAction(menuclick.EventArgs.ClickedItem)
select new { Action = action, Book = book };