使用 Rx 处理上下文菜单操作

3

我想尝试使用新的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 };

一个可能的答案涉及到SelectMany、Take和TakeUntil,但我现在还不能发布它,需要再等待5个小时。 - eddwo
请看一下我回答中的第二个选项。 - Sergey Aldoukhov
1个回答

2

解决这些问题最简单的方法是在整个可观察序列中携带引起问题的元素(从ClickedOnBook获取的那个元素)。一种方法是每次有人点击书时创建一个单独的上下文菜单实例。类似于:

IObservable<BookAction> WhenBookAction()
{
    return Observable.Defer(() => 
    {
       var menu = ContextMenuFactory.CreateMenu;
       return Observable
         .FromEventPattern<ToolStripItemClickedEventArgs>(
            menu, "ItemClicked")
         .Select(click => GetAction(click.EventArgs.ClickedItem));
    }
}

现在只需要一个简单的“来自/去往”即可:
from book in rightclicks.Select(
  click => ClickedOnBook((ListView)click.Sender, click.EventArgs))
from action in WhenBookAction()
select book, action;

您可以放心,这本书是导致菜单出现的确切书籍 - "from/from"(也称为"SelectMany"或"Monad")会处理这个问题。
编辑:这里还有另一种不错的方法。我懒得复制上下文菜单,所以我的“测试套件”是一个带有三个按钮的窗口,分别命名为“firstBn”,“secondBn”和“actionBn” - 这与此问题中的模式相同。以下是我得到的结果:
    public MainWindow()
    {
        InitializeComponent();
        var actions = Observable
            .FromEventPattern<RoutedEventArgs>(actionBn, "Click");
        var firsts = Observable
            .FromEventPattern<RoutedEventArgs>(firstBn, "Click")
            .Select(x => firstBn);
        var seconds = Observable
            .FromEventPattern<RoutedEventArgs>(secondBn, "Click")
            .Select(x => secondBn);
        var buttons = firsts.Merge(seconds);
        var buttonActions = buttons
            .Select(x => actions.Select(_ => x))
            .Switch();
        buttonActions.Subscribe(x => Console.WriteLine(x.Name));
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接