WPF 使用 WebView2
安装SDK
必须安装 Microsoft Edge (Chromium) Canary channel 浏览器
NuGet 安装 Microsoft.Web.WebView2 预发行版 0.9.538-prerelease,正式版0.9.538安装是没用的
使用
xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
<wv2:WebView2 x:Name="Browser" Source="https://www.baidu.com"/>
相关事件
SourceChanged 可以获取当前的网址,可以更新网页通过 js 修改的网址
NavigationStarting 网页加载开始
NavigationCompleted 网页加载完成
CoreWebView2Ready  CoreWebView2 初始化完成,可以进行 CoreWebView2 上的事件绑定
CoreWebView2.NewWindowRequested 加载新标签之前触发,可以通过 e.Handled = true; 阻止触发,e.Uri 为要跳转的网址
代码跳转
Browser.Source = new Uri(url);
获取源代码
主要通过 ExecuteScriptAsync 方法执行 js 代码,返回的值都是 JSON 过的。如果 JSON 编码失败则返回 null
using Newtonsoft.Json;
var html = await Browser.ExecuteScriptAsync("document.getElementsByTagName('html')[0].innerHTML");
if (string.IsNullOrWhiteSpace(html))
{
    return html;
}
return "<!DOCTYPE html><html>" + JsonConvert.DeserializeObject(html) + "</html>";
获取 cookie
using System;
using System.ComponentModel;
using System.Net;
using System.Security;
using System.Security.Permissions;
using System.Text;
/// <summary></summary>  
/// 取得WebBrowser的完整Cookie。  
/// 因为默认的webBrowser1.Document.Cookie取不到HttpOnly的Cookie  
///   
public class FullWebBrowserCookie
{
    [SecurityCritical]
    public static string GetCookieInternal(Uri uri, bool throwIfNoCookie)
    {
        uint pchCookieData = 0;
        var url = UriToString(uri);
        const uint flag = (uint)NativeMethods.InternetFlags.INTERNET_COOKIE_HTTPONLY;
        //Gets the size of the string builder  
        if (NativeMethods.InternetGetCookieEx(url, null, null, ref pchCookieData, flag, IntPtr.Zero))
        {
            pchCookieData++;
            var cookieData = new StringBuilder((int)pchCookieData);
            //Read the cookie  
            if (NativeMethods.InternetGetCookieEx(url, null, cookieData, ref pchCookieData, flag, IntPtr.Zero))
            {
                DemandWebPermission(uri);
                return cookieData.ToString();
            }
        }
        var lastErrorCode = 0;
        if (throwIfNoCookie || (lastErrorCode != (int)NativeMethods.ErrorFlags.ERROR_NO_MORE_ITEMS))
        {
            throw new Win32Exception(lastErrorCode);
        }
        return null;
    }
    private static void DemandWebPermission(Uri uri)
    {
        var uriString = UriToString(uri);
        if (uri.IsFile)
        {
            var localPath = uri.LocalPath;...剩余内容已隐藏