Microsoft Edge webView2

From wiki.visual-prolog.com

Revision as of 15:39, 14 April 2021 by Thomas Linder Puls (talk | contribs) (initial)

The webView2Demo demo program illustrates how to use the Microsoft Edge webView2 control.


WebView2Demo.png

This demo illustrates various features of the webView2control.

The webView2Control is asynchronous by design, because many things involving internet connections, etc can be long running. First of all, the control is not accessible from start and cannot really be used until it has been created and is ready. As a consequence of this most operations will have to be performed through the predicate withWebView, which takes an operation executes it with the webView2 control as argument. If the control is not ready when calling withWebView is invoked, the action will be postponed until the control becomes ready.

In the demo the ribbon commands are not activated until the control becomes available.

Above the webview2Control there is a button with the text Before and below another with the text After. If you TAB back and forth, you will start at Before and then TAB into the contents of the webView control, and eventually come to the After button. The buttons will update the status line when they have focus.

To use the control and demo it must be installed from https://go.microsoft.com/fwlink/p/?LinkId=2124703. However since the IDE also use the control you have already installed it.

setVirtualHostNameToFolderMapping is a nice feature which makes it possible to map local files to a virtual host:

        WebView:setVirtualHostNameToFolderMapping("assets.webView2Demo", @"..\assets",
            webView2_api_types::corewebview2_host_resource_access_kind_deny_cors),

With this mapping, the following navigation:

        helpHelpCmd:run := {  :- WebView:navigate("https://assets.webView2Demo/default.htm") },

will navigate to the file ..\assets\default.htm (relative to the demo executable). Files within that folder can reference each other using relative paths, so those files do not need to reference:

  • the "assets" server
  • or absolute files

The demo includes:

  • files from a virtual host
  • files from the Internet

• an html string directly from the program • using JSON RPC between the HTML and the program • handling TAB (which is just something that will happen automatically) • handling back and forward buttons (commands)

Event handlers

In its current form there are a lot of predicates named add_<something> and remove_<something>

predicates
    add_NavigationStarting : (core::predicate{iCoreWebView2NavigationStartingEventArgs} EventHandler) -> eventRegistrationToken Token.
 
predicates
    remove_NavigationStarting : (eventRegistrationToken Token).