using Microsoft.Web.WebView2.Core; using Microsoft.Web.WebView2.WinForms; using System; using System.Windows.Forms; namespace AIProofread.Controls { public partial class ProofreadMainControl : UserControl { public ProofreadMainControl() { InitializeComponent(); Bridge.InitWebEnvAsync("main", web); //this.minWidth = 420 * LabelWidth() / 42; //this.MinimumSize = new System.Drawing.Size(this.minWidth, 0); } private bool isShowingErrorPage = false; public void WebView2NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e) { if (isShowingErrorPage) { return; } if (e.IsSuccess) { isShowingErrorPage = false; return; } isShowingErrorPage = true; string errorPageHtml = @"
请检查您的网络连接或稍后再试。
重新加载 "; (sender as WebView2).CoreWebView2.NavigateToString(errorPageHtml); } private void ProofreadMainControl_Load(object sender, EventArgs e) { this.web.NavigationCompleted += WebView2NavigationCompleted; this.web.WebMessageReceived += (s, ex) => { if (ex.TryGetWebMessageAsString() == "reload") { isShowingErrorPage = false; web.Source = new Uri(Config.WebPath("correct")); } }; this.web.Source = new Uri(Config.WebPath("correct" )); //this.SizeChanged += ProofreadMainControl_SizeChanged; } private void ProofreadMainControl_SizeChanged(object sender, EventArgs e) { //if(this.minWidth > 0 && this.Width < this.minWidth) //{ // SendKeys.Send("{ESC}"); // this.Width = this.minWidth; //} } public int LabelWidth() { return label1.Width; } public void ResetWeb() { if (!this.web.IsDisposed) { this.web.Stop(); this.web.Dispose(); } } } }