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
  • 相关阅读:
    RNA velocity | RNA速率
    Dynamic networks | 动态网络
    Scale Free Network | 无标度网络
    GO | KEGG的注释是怎么来的?
    Nearest neighbor graph | 近邻图
    L0 Regularization
    Median absolute deviation | Singular Value Decomposition奇异值分解 | cumulative sums |
    Multivariate normal distribution | 多元正态分布
    相似性 similarity | Pearson | Spearman | p-value | 相关性 correlation | 距离 distance | distance measure
    Type I and type II errors | 第一类错误和第二类错误
  • 原文地址:https://www.cnblogs.com/ives/p/15148215.html
Copyright © 2011-2022 走看看