Skip to content

Added minimal WPF designer support #1989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions CefSharp.Wpf/ChromiumWebBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class ChromiumWebBrowser : ContentControl, IRenderWebBrowser, IWpfWebBrow
/// <summary>
/// The tool tip
/// </summary>
private readonly ToolTip toolTip;
private ToolTip toolTip;
/// <summary>
/// The managed cef browser adapter
/// </summary>
Expand Down Expand Up @@ -86,6 +86,11 @@ public class ChromiumWebBrowser : ContentControl, IRenderWebBrowser, IWpfWebBrow
/// The dispose count
/// </summary>
private int disposeCount;
/// <summary>
/// A flag that indicates whether or not the designer is active
/// NOTE: Needs to be static for OnApplicationExit
/// </summary>
private static bool designMode;

/// <summary>
/// Gets or sets the browser settings.
Expand Down Expand Up @@ -348,6 +353,21 @@ static ChromiumWebBrowser()
/// </summary>
/// <exception cref="System.InvalidOperationException">Cef::Initialize() failed</exception>
public ChromiumWebBrowser()
{
designMode = System.ComponentModel.DesignerProperties.GetIsInDesignMode(this);

if (!designMode)
{
InitializeFieldsAndCef();
}
}

/// <summary>
/// Required for designer support - this method cannot be inlined as the designer
/// will attempt to load libcef.dll and will subsiquently throw an exception.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
private void InitializeFieldsAndCef()
{
if (!Cef.IsInitialized && !Cef.Initialize())
{
Expand Down Expand Up @@ -414,22 +434,31 @@ public ChromiumWebBrowser()
/// </summary>
~ChromiumWebBrowser()
{
Dispose(false);
if (!designMode)
{
Dispose(false);
}
}

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
if (!designMode)
{
Dispose(true);
}

GC.SuppressFinalize(this);
}

/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="isDisposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
// 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
Expand Down Expand Up @@ -1473,7 +1502,7 @@ private void RemoveSourceHook()
/// <returns>bool to indicate if browser was created. If the browser has already been created then this will return false.</returns>
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)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we're no longer checking design mode here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CreateOffscreenBrowser gets called from OnActualSizeChanged which isn't registered when in design mode.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense 👍

{
return false;
}
Expand Down Expand Up @@ -1559,6 +1588,19 @@ private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArg
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="ExitEventArgs"/> instance containing the event data.</param>
private static void OnApplicationExit(object sender, ExitEventArgs e)
{
if (!designMode)
{
ShutdownCef();
}
}

/// <summary>
/// Required for designer support - this method cannot be inlined as the designer
/// will attempt to load libcef.dll and will subsiquently throw an exception.
/// </summary>
[MethodImpl(MethodImplOptions.NoInlining)]
private static void ShutdownCef()
{
Cef.Shutdown();
}
Expand Down