//如果没有安装则自动安装,然后再加载页面
public partial class WebView2Ex : UserControl
{
WebView2ExViewModel model;

#region 加载的URL
public string URL
{
get { return (string)GetValue(URIPropertyProperty); }
set { SetValue(URIPropertyProperty, value); }
}

// Using a DependencyProperty as the backing store for URIProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty URIPropertyProperty =
DependencyProperty.Register(nameof(URL), typeof(string), typeof(WebView2Ex), new PropertyMetadata(URLChanged));

private static void URLChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is WebView2Ex)
{
if (e.NewValue is string newURL)
{
((WebView2Ex)d).model.URL = newURL;
}
}
}
#endregion



public WebView2Ex()
{
CheckAndInstallEdge();

InitializeComponent();

this.DataContext = model = new WebView2ExViewModel();
}

private void CheckAndInstallEdge()
{
if (!CheckEdgeExist())
{
LogHelper.WriteLog("尝试安装edge 依赖");
var path = "E:\\MicrosoftEdgeWebview2Setup.exe";

//启动外部程序
Process proc = Process.Start(path);
//Process proc = Process.Start(path, "/silent /install");

if (proc != null)
{
//监视进程退出
proc.EnableRaisingEvents = true;
//指定退出事件方法
proc.WaitForExit();
//proc.Exited += new EventHandler(proc_Exited);
}
}
}

public static bool CheckEdgeExist()
{
try
{
string coreWebView2Environment = CoreWebView2Environment.GetAvailableBrowserVersionString();

return !string.IsNullOrEmpty(coreWebView2Environment);
}
catch (System.Exception e)
{
LogHelper.WriteLog("校验本地edge版本失败", e);
}

return false;
}

private async void UserControl_Loaded(object sender, RoutedEventArgs e)
{
//跨域
var path = AppDomain.CurrentDomain.BaseDirectory + "WebViewCache";
var env = await CoreWebView2Environment.CreateAsync(userDataFolder: path,
browserExecutableFolder: null,
options: new CoreWebView2EnvironmentOptions("-disable-web-security --user-data-dir=D:/ChromeDevSession"));
await webView.EnsureCoreWebView2Async(env);


webView.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;

webView.CoreWebView2.Settings.AreHostObjectsAllowed = true;


}
}

留待后查,同时方便他人