WinForms - Implement ChromiumWebBrowser.LoadError

This commit is contained in:
Alex Maitland 2021-11-11 12:47:16 +10:00
parent a56f613fae
commit 3cc3e642e9

View File

@ -35,6 +35,7 @@ namespace CefSharp.MinimalExample.WinForms
browser.StatusMessage += OnBrowserStatusMessage;
browser.TitleChanged += OnBrowserTitleChanged;
browser.AddressChanged += OnBrowserAddressChanged;
browser.LoadError += OnBrowserLoadError;
var version = string.Format("Chromium: {0}, CEF: {1}, CefSharp: {2}",
Cef.ChromiumVersion, Cef.CefVersion, Cef.CefSharpVersion);
@ -53,6 +54,34 @@ namespace CefSharp.MinimalExample.WinForms
DisplayOutput(string.Format("{0}, {1}", version, environment));
}
private async void OnBrowserLoadError(object sender, LoadErrorEventArgs e)
{
//Actions that trigger a download will raise an aborted error.
//Aborted is generally safe to ignore
if (e.ErrorCode == CefErrorCode.Aborted)
{
return;
}
//using LoadHtml/LoadUrl creates an additional history entry that
//prevents the back button from working correctly.
//Use Devools Page.SetDocumentContent to change the content
using (var client = browser.GetDevToolsClient())
{
var response = await client.Page.GetFrameTreeAsync();
var frames = response.FrameTree;
var mainFrame = frames.Frame;
var errorHtml = string.Format("<html><body><h2>Failed to load URL {0} with error {1} ({2}).</h2></body></html>",
e.FailedUrl, e.ErrorText, e.ErrorCode);
_ = client.Page.SetDocumentContentAsync(mainFrame.Id, errorHtml);
//AddressChanged isn't called for failed Urls so we need to manually update the Url TextBox
this.InvokeOnUiThreadIfRequired(() => urlTextBox.Text = e.FailedUrl);
}
}
private void OnIsBrowserInitializedChanged(object sender, EventArgs e)
{
var b = ((ChromiumWebBrowser)sender);