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
  • 相关阅读:
    09暑假总结
    给我同学的一点建议
    委托(一个主窗体统计多个从窗体的按钮单击的次数)
    关于C#写的记事本中一个问题
    IT行业最重要的四件宝我的实习体会
    使用结构、数组、循环和DataGridView写的分数统计小程序
    Visual Studio 2005 打不开,一直停在启动画面问题
    解决Cannot open the disk 'F:/vmware/Ubuntu.vmdk' or one of the snapshot disks it depends on.
    设计原则笔记
    交叉表组件
  • 原文地址:https://www.cnblogs.com/ives/p/15148215.html
Copyright © 2011-2022 走看看