2

I have standard WPF application that uses ChromiumWebBrowser CefSharp.Wpf.dll 49.0.0.0. Upgrade of the version to the latest one will be the real nightmare, hence I cannot consider it as an option. I have a small bug with opening and closing development tools:

I open my dev tools through Command Binding:

<FrameworkElement.Resources>
    <views:FreezableProxyClass x:Key="ProxyElement"
                               ProxiedDataContext="{Binding Source={x:Reference This}, Path=DataContext}" />
</FrameworkElement.Resources>
<FrameworkElement.ContextMenu>
    <ContextMenu>
        <MenuItem Command="{Binding OpenDevToolsCommand}"
                  Header="Open Chrome Dev Tools">
            <MenuItem.Icon>
                <Image Source="../../Images/dev_tools_20x20.png"
                       Width="20"
                       Height="20" />
            </MenuItem.Icon>
        </MenuItem>
    </ContextMenu>
</FrameworkElement.ContextMenu>

<FrameworkElement.InputBindings>
    <KeyBinding Key="F12"
                Command="{Binding Source={StaticResource ProxyElement}, 
            Path=ProxiedDataContext.OpenDevToolsCommand}" />

</FrameworkElement.InputBindings>
<browser:WebBrowserEx BrowserContainer="{Binding}" />

Command handler code:

  private void OpenDevToolsCommandHandler()
    {
        if (Browser.IsBrowserInitialized)
        {
            Browser.ForceShowingDevTools = !Browser.ForceShowingDevTools;
        }
        else
        {
            MessageBox.Show("Wait till the full browser initialization", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
        }
    }

And extended web browser code has ForceShowingDevTools dependency property:

 public bool ForceShowingDevTools
    {
        get { return (bool)GetValue(ForceShowingDevToolsProperty); }
        set { SetValue(ForceShowingDevToolsProperty, value); }
    }

    // Using a DependencyProperty as the backing store for ForceShowingDevTools.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ForceShowingDevToolsProperty =
        DependencyProperty.Register("ForceShowingDevTools", typeof(bool), typeof(WebBrowserEx), new PropertyMetadata(false,
            (o, e) =>
            {
                WebBrowserEx browserEx = o as WebBrowserEx;
                if (browserEx != null)
                {
                    if ((bool)e.NewValue)
                    {
                        browserEx.ShowDevTools();
                    }
                    else
                    {
                        browserEx.CloseDevTools();
                    }

                }
            }));

Whereas ShowDevTools and CloseDevTools are taken from original WebBrowserExtensions class.

It works fine when I open and close my dev tools only through context menu or F12 key binding, but my code doesn't recognize when I close dev tools by "close window" button in right top corner of dev tools window:

enter image description here

this closing doesn't inform my property, and consequently when next command invokes that handler, my application thinks that browser is opened and tries to close it. Hence I need to call my command twice to open dev tools if it was previously closed not through my command binding (from "X" button).

Is there alternative option to open and close dev tools?

Mr.B
  • 3,484
  • 2
  • 26
  • 40
  • The latest version of `CefSharp` is `57.0.0`. Are you able to replicate this using the `CefSharp.Wpf.Example`? Clone https://github.com/cefsharp/CefSharp and give it a test. :) – TEK Aug 06 '17 at 09:36
  • As I wrote above, update to the latest version is going to be a nightmare. Means, if I have a choice between update and staying with this bug - I would prefer to stay with this bug. – Mr.B Aug 07 '17 at 12:34
  • Ah, apologies. I gave it a test using the `master` branch (`58.0.0`) as I happened to have it open and I cannot replicate this. Guessing it has been fixed since `49.0.0`. I've often found upgrading `CefSharp` to be painless, but I completely understand if your environment doesn't make it so. – TEK Aug 07 '17 at 13:04
  • It doesn't make sense: any changing of CefSharp whether it is upgrade or downgrade it is too painful to consider it. thanks, anyway. – Mr.B Aug 07 '17 at 14:07

1 Answers1

0

I'd simply (and do, even with later versions of CEF Sharp) have a button/menu option to only 'Show' the dev tools and not worry about tracking whether it's open or closed.

ShowDevTools simply shows the devtools window if it's not already open, and this way you also won't need any call to CloseDevTools, or do any tracking.

If you really want to track if a window is open, you can always experiment using the SetWindowsHookEx API:

SetWindowsHookEx in C#

https://msdn.microsoft.com/en-us/library/ms644990(VS.85).aspx

..which is not something I've used myself.

VorTechS
  • 473
  • 5
  • 12