zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    小程序滴滴车主板块的银行卡管理左滑删除编辑
    超好用超短的小程序请求封装
    如何使用css来让图片居中不变形 微信小程序和web端适用
    纯css手写圆角气泡对话框 微信小程序和web都适用
    小程序getUserInfo授权升级更新登录优化
    一起聊聊3个线程依次打印1、2、3...的故事
    influxdb基础那些事儿
    influxdb的命令们
    Linux namespace浅析
    kubernetes基础概念知多少
  • 原文地址:https://www.cnblogs.com/ives/p/15148215.html
Copyright © 2011-2022 走看看