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:
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?