在自定义 ContextMenuHandler 中处理返回命令

Handle back command in custom ContextMenuHandler

我已经实现了自定义 ContextMenuHandler:

public bool OnContextMenuCommand(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
{
    if (commandId == (CefMenuCommand)26501)
    {
        // custom logic
        return true;
    }

    if (commandId == CefMenuCommand.Back)
    {
        Dispatcher.CurrentDispatcher.Invoke(() =>
        {
            if (chromiumWebBrowser.CanGoBack) chromiumWebBrowser.Back();
        });

    }

    return true;
}

自定义命令按预期工作。但是,Back-命令不起作用。该线程无权访问浏览器实例:

The calling thread cannot access this object because a different thread owns it.

省略调度逻辑并不能解决问题。

  1. 我是否需要使用 if 语句显式处理 Back-命令(将被重构为 swicth-case,不用担心)
  2. 如果是,怎么做?
  3. 一般来说,如何获取网络浏览器实例的调度程序?

解决方案是不在 switch-case 中包含 Back-命令,而是 return false。

public bool OnContextMenuCommand(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
{
    if (commandId == (CefMenuCommand)26501)
    {
        // custom logic
        return true;
    } 

    return false;
}