环境:VS2015+cefsharp 57.0
全部代码如下:
1、要初始化,否则不能刷新
using System; using CefSharp; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using CefSharp.WinForms; namespace mycef { public partial class Form1 : Form { public ChromiumWebBrowser webBrower; public Form1() { InitializeComponent(); SetInit(); } private void Form1_Load(object sender, EventArgs e) { string path = "www.029gc.cn"; webBrower = new ChromiumWebBrowser(path); webBrower.LifeSpanHandler = new OpenPageSelf(); webBrower.Dock = DockStyle.Fill;//填充方式 this.Controls.Add(webBrower); } public static void SetInit() { CefSettings setting = new CefSettings(); Cef.Initialize(setting); } private void button3_Click(object sender, EventArgs e) { webBrower.Reload(true); } private void button1_Click(object sender, EventArgs e) { webBrower.GetMainFrame().Browser.GoBack(); } private void button2_Click(object sender, EventArgs e) { webBrower.GetMainFrame().Browser.GoForward(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { } } internal class OpenPageSelf : ILifeSpanHandler { public bool DoClose(IWebBrowser browserControl, IBrowser browser) { return false; } public void OnAfterCreated(IWebBrowser browserControl, IBrowser browser) { } public void OnBeforeClose(IWebBrowser browserControl, IBrowser browser) { } public bool OnBeforePopup(IWebBrowser browserControl, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser) { newBrowser = null; var chromiumWebBrowser = (ChromiumWebBrowser)browserControl; chromiumWebBrowser.Load(targetUrl); return true; } } }