/// <summary> /// Programatically initialize the WebView2Wrapper component. /// </summary> public async void InitializeWebView(DockPanel docker) { _webView = new Microsoft.Web.WebView2.Wpf.WebView2(); // clear everything in the WPF dock panel container docker.Children.Clear(); docker.Children.Add(_webView); // initialize the webview 2 instance try { _webView.CoreWebView2InitializationCompleted += OnWebViewInitializationCompleted; CoreWebView2Environment env = await CoreWebView2Environment.CreateAsync(null, ExecutingLocation); await _webView.EnsureCoreWebView2Async(env); _webView.WebMessageReceived += OnWebViewInteraction; _webView.NavigationCompleted += OnWebViewNavigationCompleted; //InitializeDevToolsProtocolHelper(); //SubscribeToDocumentUpdated(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public HtmlRenderer(System.Windows.Controls.Panel controlPanel) { // setup and host the internet explorer web browser that is built into the .net Windows Forms framework try { _myBrowser = new Microsoft.Web.WebView2.Wpf.WebView2(); bool navigateFiredOnce = false; _myBrowser.NavigationStarting += (sender, args) => { // suppress script errors if (navigateFiredOnce == false) { // do whatever you might want to do only one time, in the entire browser navigation cycle // Used to do the suppress script errors of IE here // TODO: Probably remove this whole code block later navigateFiredOnce = true; } }; _myBrowser.NavigationCompleted += async(sender, args) => { // TODO: Need to setup document click stuff // We lost all the IE document selection stuff, so would need to do that with javascript and events and stuff }; controlPanel.Children.Add(_myBrowser); } catch (Exception ex) { System.Windows.MessageBox.Show($"Problem occured loading browser. Exception: {ex}", "Browser Load Failed", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); } }
public WpfWebView2Wrapper(WebView2Control webView2) { if (webView2 is null) { throw new ArgumentNullException(nameof(webView2)); } WebView2 = webView2; _coreWebView2Wrapper = new WpfCoreWebView2Wrapper(this); }
private void ButtonGo_Click(object sender, RoutedEventArgs e) { Microsoft.Web.WebView2.Wpf.WebView2 webView = (A_or_B ? webViewB : webViewA); if (webView != null && webView.CoreWebView2 != null) { webView.CoreWebView2.Navigate(addressBar.Text); A_or_B = !A_or_B; ButtonGo.Content = (A_or_B ? "Go [B]" : "Go [A]"); } }
void EnsureHttps(object sender, CoreWebView2NavigationStartingEventArgs args) { Microsoft.Web.WebView2.Wpf.WebView2 webView = sender as Microsoft.Web.WebView2.Wpf.WebView2; String uri = args.Uri; if (!uri.StartsWith("https://")) { webView.CoreWebView2.ExecuteScriptAsync($"alert('{uri} is not safe, try an https link')"); args.Cancel = true; } }
/// <summary> /// Creates instance of this class. /// </summary> /// <param name="url">URL to navigate to. This URL must redirect to a log-in page.</param> public WebBrowserLogin(Uri url, IWebRequestAsync request, WebDavSessionAsync davClient, ILog log) { this.url = url; this.request = request; this.davClient = davClient; this.log = log; InitializeComponent(); this.webView = new Microsoft.Web.WebView2.Wpf.WebView2(); this.Loaded += WebBrowserLogin_Load; this.panel.Children.Add(this.webView); }
public void Show() { this.window = new System.Windows.Window(); this.webview = new Microsoft.Web.WebView2.Wpf.WebView2(); this.window.Content = this.webview; this.window.Loaded += async(sender, e) => { string scripts = LoadAllJavaScript(); await this.webview.EnsureCoreWebView2Async(); this.webview.NavigateToString("<html><head>" + scripts + "\n\n</head><body>Hello, World!</body></html>"); }; }
internal override Task <string> CreateAndShowWindowImpl(string title, byte[] nullableIcon, int width, int height, Func <string, string, bool> handleVmBoundMessage) { TaskCompletionSource <string> completionTask = new TaskCompletionSource <string>(); System.Threading.Thread thread = new System.Threading.Thread(() => { this.nativeWindow = new System.Windows.Window() { Title = title, Width = width, Height = height }; this.webview = new Microsoft.Web.WebView2.Wpf.WebView2(); this.nativeWindow.Content = this.webview; if (nullableIcon != null) { this.nativeWindow.Icon = System.Windows.Media.Imaging.BitmapFrame.Create(new System.IO.MemoryStream(nullableIcon)); } this.nativeWindow.Loaded += async(sender, e) => { await this.webview.EnsureCoreWebView2Async(); this.dispatcher = this.nativeWindow.Dispatcher; this.webview.CoreWebView2.AddHostObjectToScript("u3bridge", new JsBridge((type, payloadJson) => { handleVmBoundMessage(type, payloadJson); return("{}"); })); LoadHtmlInWebview(); }; this.ApplyCloseCauseHandlers(); this.nativeWindow.ShowDialog(); completionTask.TrySetResult(this.closeCause); }); thread.SetApartmentState(System.Threading.ApartmentState.STA); thread.Start(); return(completionTask.Task); }
public WpfEvents(Microsoft.Web.WebView2.Wpf.WebView2 webView) { this.webView = webView; }
public WpfWeb2ViewWrapper(WebView2Control webView2) { _webView2 = webView2 ?? throw new ArgumentNullException(nameof(webView2)); }
public static async Task InitAsync(Microsoft.Web.WebView2.Wpf.WebView2 webView) => await InitAsync(webView.GetCoreWebView());