1、项目引用nuget
2、winform窗体中初始化
var webView = new WebView2();
webView.Source = new Uri(url);
webView.Dock = DockStyle.Fill;
//接收js调用c#函数的消息
webView.WebMessageReceived += CoreWebView2_WebMessageReceivedAsync;
this.panel1.Controls.Add(webView);
//接受js调用c#代码
private async void CoreWebView2_WebMessageReceivedAsync(object sender, CoreWebView2WebMessageReceivedEventArgs e)
{
try
{
function();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
3、webView2中的js调用c#代码
window.chrome.webview.postMessage(123);