webview2 winform/wpf用法

WebView2 webView2 = new WebView2();
private async void web2_LoadAsync(object sender, EventArgs e)
{
    if (!string.IsNullOrWhiteSpace(AccessibleDescription))
    {
        await InitializeWebView2Async(AccessibleDescription);
    }
    else
    {
        await InitializeWebView2Async("http://172.16.20.3:8060/");
    }
}

private async Task InitializeWebView2Async(string url)
{
    try
    {
        await webView2.EnsureCoreWebView2Async(); //检查webview2安装状态
        this.Controls.Add(webView2);
        webView2.Dock= DockStyle.Fill;
        webView2.CoreWebView2.Navigate(url);
    }
    catch (Exception ex)
    {
        MessageBox.Show($"WebView2初始化检查失败:{ex.Message}\n请先安装WebView2运行时,下载地址:\nhttps://developer.microsoft.com/zh-cn/microsoft-edge/webview2/\n安装器:https://go.microsoft.com/fwlink/p/?LinkId=2124703");
        Process.Start("https://go.microsoft.com/fwlink/p/?LinkId=2124703");
    }
}
View Code

1,webview2加载地址前,先检测安装状态。

2,附带发行组件:Microsoft.Web.WebView2.Core.dll , Microsoft.Web.WebView2.WinForms.dll , Microsoft.Web.WebView2.Wpf.dll

3,附带runtimes: 

  \runtimes\win-x86\native\WebView2Loader.dll

  \runtimes\win-x64\native\WebView2Loader.dll

 

posted @ 2025-07-16 10:12  enif  阅读(48)  评论(0)    收藏  举报
豫ICP备2021034901号