public Form(int width, int height) { components = new System.ComponentModel.Container(); AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; ClientSize = new System.Drawing.Size(width, height); Text = "Form1"; GraphicsPanel = new FocusablePanel(); GraphicsPanel.Dock = System.Windows.Forms.DockStyle.Fill; GraphicsPanel.BackColor = System.Drawing.Color.Black; Controls.Add(GraphicsPanel); var uiPanel = new System.Windows.Forms.TableLayoutPanel(); uiPanel.Dock = System.Windows.Forms.DockStyle.Right; uiPanel.Width = 240; Controls.Add(uiPanel); // Requires Edge (Chromium): // https://www.microsoftedgeinsider.com/en-us/download WebView = new Microsoft.Web.WebView2.WinForms.WebView2(); WebView.Location = System.Drawing.Point.Empty; uiPanel.Controls.Add(WebView); WebView.Margin = WebView.Padding = System.Windows.Forms.Padding.Empty; WebView.Size = uiPanel.Size; uiPanel.Resize += (object sender, System.EventArgs e) => { WebView.Size = uiPanel.Size; }; InitializeAsync(WebView); }
private async Task LoadHtmlAsync(Microsoft.Web.WebView2.WinForms.WebView2 wv) { string click = "document.querySelector(\"[data-name = '2d_w']\").click();"; string html = ""; var o = await wv.CoreWebView2.ExecuteScriptAsync(click); o = await wv.CoreWebView2.ExecuteScriptAsync("let g =document.querySelectorAll('#tabid_1_content_div svg g g');let result = '';for (let x of g) { result += x.getAttribute('transform') + ';'; };result"); html = o.ToString(); JArray ja = WRFparser.WRFparser.Parse(html); Outputs.Add(new JObject( new JProperty("name", wv.Tag), new JProperty("wind", ja) )); /* * WRFparser.WRFparser.DebugTest = Config.Debug; * WRFparser.WRFparser.Time = Config.Time; * WRFparser.WRFparser.Name = wv.Tag.ToString(); * JArray ja = WRFparser.WRFparser.Parse(html); * outputs.Add(new JObject( * new JProperty("name", wv.Tag), * new JProperty("wind", ja) * )); */ }
public WebView2Handler() { WebView2Loader.EnsureWebView2Runtime(); Control = new WebView2Control(); Control.CoreWebView2Ready += Control_CoreWebView2Ready; InitializeAsync(); }
private void RunWeb(string url, string name) { Microsoft.Web.WebView2.WinForms.WebView2 wv = new Microsoft.Web.WebView2.WinForms.WebView2(); wv.Source = new Uri(url); wv.Tag = name; wv.NavigationCompleted += webView_NavigationCompleted; }
void AttachControlEventHandlers(Microsoft.Web.WebView2.WinForms.WebView2 control) { control.CoreWebView2InitializationCompleted += WebView2Control_CoreWebView2InitializationCompleted; control.NavigationStarting += WebView2Control_NavigationStarting; control.NavigationCompleted += WebView2Control_NavigationCompleted; control.SourceChanged += WebView2Control_SourceChanged; control.KeyDown += WebView2Control_KeyDown; control.KeyUp += WebView2Control_KeyUp; }
public WindowsFormsWebView2Wrapper(WebView2Control webView2) { if (webView2 is null) { throw new ArgumentNullException(nameof(webView2)); } _webView2 = webView2; }
/// <summary> /// Constructs an instance of <see cref="WebView2WebViewManager"/>. /// </summary> /// <param name="webview">A <see cref="WebView2Control"/> to access platform-specific WebView2 APIs.</param> /// <param name="services">A service provider containing services to be used by this class and also by application code.</param> /// <param name="dispatcher">A <see cref="Dispatcher"/> instance that can marshal calls to the required thread or sync context.</param> /// <param name="fileProvider">Provides static content to the webview.</param> /// <param name="hostPageRelativePath">Path to the host page within the <paramref name="fileProvider"/>.</param> public WebView2WebViewManager(WebView2Control webview, IServiceProvider services, Dispatcher dispatcher, IFileProvider fileProvider, JSComponentConfigurationStore jsComponents, string hostPageRelativePath) : base(services, dispatcher, new Uri(AppOrigin), fileProvider, jsComponents, hostPageRelativePath) { _webview = webview ?? throw new ArgumentNullException(nameof(webview)); // Unfortunately the CoreWebView2 can only be instantiated asynchronously. // We want the external API to behave as if initalization is synchronous, // so keep track of a task we can await during LoadUri. _webviewReadyTask = InitializeWebView2(); }
public WindowsFormsWebView2Wrapper(WebView2Control webView2) { if (webView2 is null) { throw new ArgumentNullException(nameof(webView2)); } WebView2 = webView2; _coreWebView2Wrapper = new WindowsFormsCoreWebView2Wrapper(this); }
private void RunWeb(string url, string name) { using (Microsoft.Web.WebView2.WinForms.WebView2 wv = new Microsoft.Web.WebView2.WinForms.WebView2()) { wv.Source = new Uri(url); wv.Tag = name; wv.NavigationCompleted += webView_NavigationCompleted; Thread.Sleep(WRFparser.WRFparser.Config.Delay * 2); } }
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())); }
/// <summary> /// 删除请求事件 /// </summary> /// <param name="webview">WinForms.WebView2</param> /// <param name="handler">handler</param> /// <returns>WinForms.WebView2</returns> public static WinForms.WebView2 RemoveRequestHandler(this Microsoft.Web.WebView2.WinForms.WebView2 webview, Action <CoreWebView2WebResourceRequestedEventArgs> handler) { if (RequestHandlers.ContainsKey(nameof(handler))) { RequestHandlers.Remove(nameof(handler)); } return(webview); }
/// <summary> /// Creates a new instance of <see cref="BlazorWebView"/>. /// </summary> public BlazorWebView() { Dispatcher = new WindowsFormsDispatcher(this); RootComponents.CollectionChanged += HandleRootComponentsCollectionChanged; _webview = new WebView2Control() { Dock = DockStyle.Fill, }; Controls.Add(_webview); }
public WebView2Handler() { WebView2Loader.EnsureWebView2Runtime(); Control = new WebView2Control(); Control.CoreWebView2InitializationCompleted += Control_CoreWebView2Ready; #if WPF _host = new WebView2Host(); _host.Child = Control; _host.Handler = this; #endif }
/// <summary> /// 注册API域名 拦截 /// </summary> /// <param name="webview">WinForms.WebView2</param> /// <param name="domain">需要拦截的域名 默认 http://api.app.local </param> /// <returns>WinForms.WebView2</returns> public static WinForms.WebView2 RegisterApiDomain(this Microsoft.Web.WebView2.WinForms.WebView2 webview, string domain = "http://api.app.local") { ApiDomain = domain; webview.CoreWebView2.AddWebResourceRequestedFilter($"{ApiDomain}/*", CoreWebView2WebResourceContext.All); webview.CoreWebView2.WebResourceRequested += CoreWebView2OnWebResourceRequested; if (!RequestHandlers.ContainsKey(nameof(WebApiRequested))) { RequestHandlers.Add(nameof(WebApiRequested), WebApiRequested); } return(webview); }
/// <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); } }
public FrmMainWindow() { InitializeComponent(); var webview = new Microsoft.Web.WebView2.WinForms.WebView2() { Dock = DockStyle.Fill }; this.Controls.Add(webview); this.Size = new Size(1600, 900); this.InitializeWebview2Async(webview, null, null, null, () => AppRuntime.RunTime.RegisterWebViewControl(webview).GoUri(webview, new Uri("http://main.app.service/index.html"))); //.GoUri(webview,new Uri("http://main.app.local/wwwroot/index.html")) // webview.Source = new Uri(Path.Combine(Path.GetDirectoryName(this.GetType().Assembly.Location), "wwwroot","index.html")); }
/// <summary> /// 注册数据模型 /// </summary> /// <param name="webview">WinForms.WebView2</param> /// <param name="assembly">Assembly</param> /// <returns>WinForms.WebView2</returns> public static WinForms.WebView2 RegisterDataModels(this Microsoft.Web.WebView2.WinForms.WebView2 webview, Assembly assembly) { Routes.AddRange(new DataModelProvider().ImportDataModelAssembly(assembly)); return(webview); }
/// <summary> /// 注册C#对象到脚本 /// </summary> /// <param name="webview">WinForms.WebView2</param> /// <param name="name">对象名称</param> /// <param name="obj">obj</param> /// <returns>WinForms.WebView2</returns> public static WinForms.WebView2 RegisterObjectToScript(this Microsoft.Web.WebView2.WinForms.WebView2 webview, string name, object obj) { webview.CoreWebView2.AddHostObjectToScript(name, obj); return(webview); }
public WinFormsEvents(Microsoft.Web.WebView2.WinForms.WebView2 webView) { this.webView = webView; }
public WebView2Handler() { WebView2Loader.EnsureWebView2Runtime(); Control = new WebView2Control(); Control.CoreWebView2InitializationCompleted += Control_CoreWebView2Ready; }
public static async Task InitAsync(Microsoft.Web.WebView2.WinForms.WebView2 webView) => await InitAsync(webView.GetCoreWebView());