• c# edge webview2封装 解决跨域问题


    //如果没有安装则自动安装,然后再加载页面
        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;
    
                  
            }
        }
    
    
    留待后查,同时方便他人
    联系我:renhanlinbsl@163.com
  • 相关阅读:
    【C++】C++类模板和函数模板
    【C++】 const补充
    C# 版本雪花算法(SnowFlake)
    Could not load file or assembly 'System.Windows.Forms, Version=6.0.2.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
    VMware Workstation Pro v16.2.0 官方完整版(附永久激活密钥)
    beego等6款超好用golang web框架
    Docker部署Golang
    php提前结束http响应
    redis导出导入
    json数据类型 相关知识
  • 原文地址:https://www.cnblogs.com/ives/p/15148215.html
Copyright © 2020-2023  润新知