|
|
Posted on 2022-9-25 23:02:27
|
|
|
|

Requirements: Create a new .NET Framework 4.7.2 WinForm project with VS 2022, reference the Microsoft.Web.WebView2 SDK package, and use the WebView2 control to display and render the web app. The Microsoft Edge WebView2 control allows embedding web technologies (HTML, CSS, and JavaScript) in native apps. The WebView2 control uses Microsoft Edge as the drawing engine to display web content in native apps.
Review:
WebView2 runtime
If your Windows system does not have the WebView2 runtime installed (Windows 11 system is already installed by default), the first time you need to download it manually, address:The hyperlink login is visible.
Create a new WinForm project
Open VS 2022 to create a new WinForm project for .NET Framework 4.7.2, after creating it, you need to reference the Microsoft.Web.WebView2 package, the nuget command is as follows:
In the toolbox, find the WebView2 control under Windows Forms Control and drag it to the Form1 form, as shown in the following figure:
Tip: By default, in Visual Studio 2017, WebView2 doesn't appear in the Toolbox. To make WebView2 appear in the toolbox, select the Tools > option > General> and set the Autofill toolbox setting to true.
Set up the webView21 controlDock: Fill,Source:https://www.itsvse.com/, as shown in the figure below:
Start the project and you can see that the WinForm application successfully loads and displays the web content, as shown in the following image:
WinForm application size
One of the biggest concerns is the size of the application, after creating a new WinForm application that references the WebView2 SDK, the size of the program with almost no code written is:1.36M, as shown in the figure below:
Displays the WebView2 runtime version
How do I get through the .NET program whether the system has the WebView2 runtime or the version of the WebView2 runtime? The code is as follows:
invokeGetAvailableBrowserVersionString() methodabnormal, which means that the WebView2 runtime is not installed。
For information about distributing apps and WebView2 runtime, see:The hyperlink login is visible.
Enable developer tools
How do I enable developer tools when I need to co-commission a web application with WinForm, sometimes I need to look at DOM elements or network resource requests?
You can open the DevTools Developer Tools using the F12 method in the webview2 control after startup, or you can open it with the following code:
As shown below:
Disable right-click menus and developer tools
In a real production environment, when we deploy the application client on the customer's machine,You don't want users to be able to right-click (refresh, view page source code, etc.) and disable developer tools and page zoom controls, the code is as follows:
Microsoft.Web.WebView2.Core API documentation:The hyperlink login is visible. Microsoft.Web.WebView2.WinForms API documentation:The hyperlink login is visible.
Source code download:
Tourists, if you want to see the hidden content of this post, please Reply
(End)
|
Previous:[WebView2] (1) Initial introduction to Microsoft Edge WebView2 technologyNext:【Performance Optimization】Preconnect, DNS-Prefetch, and Preload features on the front-end
|