Files
cefsharp/CefSharp.WinForms.Example/Minimal/SimpleBrowserForm.cs
T
amaitland e4312760ce API Change - IJavascriptObjectRepository.Register remove isAsync default param
**BREAKING CHANGE**

- You must now explicitly specify the `isAsync` param.
- Add additional xml doc comment regarding .Net Core and using only isAsync = true

The isAsync = false option (sync binding) requires WCF which isn't available on .Net core, remove the default value so you are forced to specify an option and in the process hopefully read the documentation.

For those who previously hadn't provided a param then simply pass in false for the third argument.
e.g.
browser.JavascriptObjectRepository.Register("bound", new BoundObject(), false);

Resolves #3016
2020-01-21 08:39:23 +10:00

198 lines
6.0 KiB
C#

// Copyright © 2010 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.ComponentModel;
using System.Windows.Forms;
using CefSharp.Example.JavascriptBinding;
namespace CefSharp.WinForms.Example.Minimal
{
public partial class SimpleBrowserForm : Form
{
private ChromiumWebBrowser browser;
private IFocusHandler customFocusHandler;
private bool multiThreadedMessageLoop;
public SimpleBrowserForm(bool multiThreadedMessageLoop, IFocusHandler customFocusHandler = null)
{
InitializeComponent();
this.customFocusHandler = customFocusHandler;
this.multiThreadedMessageLoop = multiThreadedMessageLoop;
Text = "CefSharp";
WindowState = FormWindowState.Maximized;
var bitness = Environment.Is64BitProcess ? "x64" : "x86";
var version = String.Format("Chromium: {0}, CEF: {1}, CefSharp: {2}, Environment: {3}", Cef.ChromiumVersion, Cef.CefVersion, Cef.CefSharpVersion, bitness);
DisplayOutput(version);
//Only perform layout when control has completly finished resizing
ResizeBegin += (s, e) => SuspendLayout();
ResizeEnd += (s, e) => ResumeLayout(true);
Load += OnLoad;
}
public IContainer Components
{
get
{
if (components == null)
{
components = new Container();
}
return components;
}
}
private void OnLoad(object sender, EventArgs e)
{
CreateBrowser();
}
private void CreateBrowser()
{
browser = new ChromiumWebBrowser("www.google.com")
{
Dock = DockStyle.Fill,
};
toolStripContainer.ContentPanel.Controls.Add(browser);
browser.LoadingStateChanged += OnBrowserLoadingStateChanged;
browser.ConsoleMessage += OnBrowserConsoleMessage;
browser.StatusMessage += OnBrowserStatusMessage;
browser.TitleChanged += OnBrowserTitleChanged;
browser.AddressChanged += OnBrowserAddressChanged;
browser.JavascriptObjectRepository.Register("bound", new BoundObject(), false);
if (!multiThreadedMessageLoop)
{
browser.FocusHandler = null;
}
//Only override if we have a custom handler
if (customFocusHandler != null)
{
browser.FocusHandler = customFocusHandler;
}
}
private void OnBrowserConsoleMessage(object sender, ConsoleMessageEventArgs args)
{
DisplayOutput(string.Format("Line: {0}, Source: {1}, Message: {2}", args.Line, args.Source, args.Message));
}
private void OnBrowserStatusMessage(object sender, StatusMessageEventArgs args)
{
this.InvokeOnUiThreadIfRequired(() => statusLabel.Text = args.Value);
}
private void OnBrowserLoadingStateChanged(object sender, LoadingStateChangedEventArgs args)
{
SetCanGoBack(args.CanGoBack);
SetCanGoForward(args.CanGoForward);
this.InvokeOnUiThreadIfRequired(() => SetIsLoading(!args.CanReload));
}
private void OnBrowserTitleChanged(object sender, TitleChangedEventArgs args)
{
this.InvokeOnUiThreadIfRequired(() => Text = args.Title);
}
private void OnBrowserAddressChanged(object sender, AddressChangedEventArgs args)
{
this.InvokeOnUiThreadIfRequired(() => urlTextBox.Text = args.Address);
}
private void SetCanGoBack(bool canGoBack)
{
this.InvokeOnUiThreadIfRequired(() => backButton.Enabled = canGoBack);
}
private void SetCanGoForward(bool canGoForward)
{
this.InvokeOnUiThreadIfRequired(() => forwardButton.Enabled = canGoForward);
}
private void SetIsLoading(bool isLoading)
{
goButton.Text = isLoading ?
"Stop" :
"Go";
goButton.Image = isLoading ?
Properties.Resources.nav_plain_red :
Properties.Resources.nav_plain_green;
HandleToolStripLayout();
}
public void DisplayOutput(string output)
{
this.InvokeOnUiThreadIfRequired(() => outputLabel.Text = output);
}
private void HandleToolStripLayout(object sender, LayoutEventArgs e)
{
HandleToolStripLayout();
}
private void HandleToolStripLayout()
{
var width = toolStrip1.Width;
foreach (ToolStripItem item in toolStrip1.Items)
{
if (item != urlTextBox)
{
width -= item.Width - item.Margin.Horizontal;
}
}
urlTextBox.Width = Math.Max(0, width - urlTextBox.Margin.Horizontal - 18);
}
private void ExitMenuItemClick(object sender, EventArgs e)
{
browser.Dispose();
Cef.Shutdown();
Close();
}
private void GoButtonClick(object sender, EventArgs e)
{
LoadUrl(urlTextBox.Text);
}
private void BackButtonClick(object sender, EventArgs e)
{
browser.Back();
}
private void ForwardButtonClick(object sender, EventArgs e)
{
browser.Forward();
}
private void UrlTextBoxKeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode != Keys.Enter)
{
return;
}
LoadUrl(urlTextBox.Text);
}
private void LoadUrl(string url)
{
if (Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
{
browser.Load(url);
}
}
}
}