diff --git a/CefSharp.Wpf/ChromiumWebBrowser.cs b/CefSharp.Wpf/ChromiumWebBrowser.cs index 38efb4952e..ec66e53651 100644 --- a/CefSharp.Wpf/ChromiumWebBrowser.cs +++ b/CefSharp.Wpf/ChromiumWebBrowser.cs @@ -45,7 +45,7 @@ public class ChromiumWebBrowser : ContentControl, IRenderWebBrowser, IWpfWebBrow /// /// The tool tip /// - private readonly ToolTip toolTip; + private ToolTip toolTip; /// /// The managed cef browser adapter /// @@ -86,6 +86,11 @@ public class ChromiumWebBrowser : ContentControl, IRenderWebBrowser, IWpfWebBrow /// The dispose count /// private int disposeCount; + /// + /// A flag that indicates whether or not the designer is active + /// NOTE: Needs to be static for OnApplicationExit + /// + private static bool designMode; /// /// Gets or sets the browser settings. @@ -348,6 +353,21 @@ static ChromiumWebBrowser() /// /// Cef::Initialize() failed public ChromiumWebBrowser() + { + designMode = System.ComponentModel.DesignerProperties.GetIsInDesignMode(this); + + if (!designMode) + { + InitializeFieldsAndCef(); + } + } + + /// + /// Required for designer support - this method cannot be inlined as the designer + /// will attempt to load libcef.dll and will subsiquently throw an exception. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + private void InitializeFieldsAndCef() { if (!Cef.IsInitialized && !Cef.Initialize()) { @@ -414,7 +434,10 @@ public ChromiumWebBrowser() /// ~ChromiumWebBrowser() { - Dispose(false); + if (!designMode) + { + Dispose(false); + } } /// @@ -422,7 +445,11 @@ public ChromiumWebBrowser() /// public void Dispose() { - Dispose(true); + if (!designMode) + { + Dispose(true); + } + GC.SuppressFinalize(this); } @@ -430,6 +457,8 @@ public void Dispose() /// Releases unmanaged and - optionally - managed resources. /// /// true to release both managed and unmanaged resources; false to release only unmanaged resources. + // This method cannot be inlined as the designer will attempt to load libcef.dll and will subsiquently throw an exception. + [MethodImpl(MethodImplOptions.NoInlining)] protected virtual void Dispose(bool isDisposing) { //If disposeCount is 0 then we'll update it to 1 and begin disposing @@ -1473,7 +1502,7 @@ private void RemoveSourceHook() /// bool to indicate if browser was created. If the browser has already been created then this will return false. protected virtual bool CreateOffscreenBrowser(Size size) { - if (browserCreated || System.ComponentModel.DesignerProperties.GetIsInDesignMode(this) || size.IsEmpty || size.Equals(new Size(0, 0))) + if (browserCreated || size.IsEmpty || size.Equals(new Size(0, 0))) { return false; } @@ -1559,6 +1588,19 @@ private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArg /// The sender. /// The instance containing the event data. private static void OnApplicationExit(object sender, ExitEventArgs e) + { + if (!designMode) + { + ShutdownCef(); + } + } + + /// + /// Required for designer support - this method cannot be inlined as the designer + /// will attempt to load libcef.dll and will subsiquently throw an exception. + /// + [MethodImpl(MethodImplOptions.NoInlining)] + private static void ShutdownCef() { Cef.Shutdown(); }