.NET、WinForms。
\n调用是从 UI 线程触发的(按钮 - 单击)。ExecuteScriptAsync 的返回应继续同步处理,即它们应再次与调用上下文同步。我在这里失败了。
\n我尝试过例如:
\nprivate void buttonTest1_Click(object sender, EventArgs e) {\n MessageBox.Show(GetMathResult());\n }\n\n String GetMathResult() {\n // a) Application freezes\n //var result = webView.ExecuteScriptAsync("Math.sin(Math.PI/2)").GetAwaiter().GetResult();\n //return result;\n\n // b) return null\n //String result = null;\n //Task task = new Task(async () => { result = await webView.ExecuteScriptAsync("Math.sin(Math.PI/2)"); }); \n //task.RunSynchronously();\n //return result;\n\n // c) Excepion: // InvalidCastException: Das COM-Objekt des Typs "System.__ComObject" kann nicht in den Schnittstellentyp "Microsoft.Web.WebView2.Core.Raw.ICoreWebView2Controller" umgewandelt werden. Dieser Vorgang konnte nicht …Run Code Online (Sandbox Code Playgroud)