Пример #1
0
        async void InitializeAsync(Microsoft.Web.WebView2.WinForms.WebView2 webView)
        {
            var localAppData = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData);
            var cacheFolder  = System.IO.Path.Combine(localAppData, "WindowsFormsWebView2");
            var environment  = await Microsoft.Web.WebView2.Core.CoreWebView2Environment.CreateAsync(userDataFolder : cacheFolder);

            await webView.EnsureCoreWebView2Async(environment);

            webView.CoreWebView2.Navigate(System.String.Format("file:///{0}/resources/index.html", System.IO.Directory.GetCurrentDirectory()));
        }
Пример #2
0
        /// <summary>
        /// Webview2 初始化
        /// </summary>
        /// <param name="webview">Microsoft.Web.WebView2.WinForms.WebView2</param>
        /// <param name="browserExecutableFolder">browserExecutableFolder</param>
        /// <param name="userDataFolder">userDataFolder</param>
        /// <param name="options">CoreWebView2EnvironmentOptions</param>
        /// <param name="func">Func</param>
        public async Task InitializeWebview2Async(Microsoft.Web.WebView2.WinForms.WebView2 webview,
                                                  string browserExecutableFolder         = null,
                                                  string userDataFolder                  = null,
                                                  CoreWebView2EnvironmentOptions options = null,
                                                  Func <AppRuntime> func                 = null)
        {
            userDataFolder = userDataFolder ?? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "UserData");
            AppRuntime.RunTime.WebView2Environment = await CoreWebView2Environment.CreateAsync(browserExecutableFolder, userDataFolder, options);

            await webview.EnsureCoreWebView2Async(AppRuntime.RunTime.WebView2Environment);

            if (func != null)
            {
                webview.Invoke(func);
            }
        }
Пример #3
0
        private async Task InitializeWebView2()
        {
            _coreWebView2Environment = await CoreWebView2Environment.CreateAsync()
#if WEBVIEW2_MAUI
                                       .AsTask()
#endif
                                       .ConfigureAwait(true);

            await _webview.EnsureCoreWebView2Async();

            ApplyDefaultWebViewSettings();

            _webview.CoreWebView2.AddWebResourceRequestedFilter($"{AppOrigin}*", CoreWebView2WebResourceContext.All);
            _webview.CoreWebView2.WebResourceRequested += async(s, eventArgs) =>
            {
                await HandleWebResourceRequest(eventArgs);
            };

            // The code inside blazor.webview.js is meant to be agnostic to specific webview technologies,
            // so the following is an adaptor from blazor.webview.js conventions to WebView2 APIs
            await _webview.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(@"
                window.external = {
                    sendMessage: message => {
                        window.chrome.webview.postMessage(message);
                    },
                    receiveMessage: callback => {
                        window.chrome.webview.addEventListener('message', e => callback(e.data));
                    }
                };
            ")
#if WEBVIEW2_MAUI
            .AsTask()
#endif
            .ConfigureAwait(true);

            QueueBlazorStart();

            _webview.CoreWebView2.WebMessageReceived += (s, e) => MessageReceived(new Uri(e.Source), e.TryGetWebMessageAsString());
        }