Webview2

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 1 month and 4 days old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
mark106
Posts: 32
Last visit: Wed Oct 09, 2024 5:36 am
Has voted: 1 time

Webview2

Post by mark106 »

Product: PowerShell Studio 2024 (64 Bit)
Build: v5.8.246
OS: Windows 10.0.19045
PS Version(s): 5.1.19041.1, 7.4.2, 7.3.4

As a bit of a novice, particularly in the Web side of things, I thought I'd share a couple points, that I struggled with, in case they are useful for others getting started with the WebView2 Control - or this may yield a better way of doing things from others.

Firstly, with regards to this previous post....
viewtopic.php?p=85777#p85777
..... many thanks for the changes made. The Webview2 control seems to be working well in PowerShell Studio now :D

When I looked, I couldn't find much information out there on using the control with PowerShell and was struggling to work out to interact with the Web page/site. With the old WebBrowser control I would often drill down through the document property of the object to get properties like innerHTML for example.

Code: Select all

$webbrowser1.document.activeelement.InnerHtml
However, as I understand it, with the WebView2 Control, all Web page/site interaction is done via Javascript commands using the ExecuteScriptAsync method. Results can be returned using postMessage() and these appear in (and trigger) the WebMessageReceived Event.

So, as a rough equivalent to the above line to get InnerHTML.....

Code: Select all

$WebView21.ExecuteScriptAsync("window.chrome.webview.postMessage(document.documentElement.innerHTML);");
seems to work well.


corewebview2
When I dropped the WebVIew2 control into an existing project, I did have problems with the control failing to initialise. Even though I was using the Initialize-WebView2Control helper function ("triggered from form_load) the initialisation failed with a corewebview2 in use error. It turned out this was due to me hard-coding a source (URL) in the GUI (for initial testing).

If no source (URL) is set in the PSS GUI then the initialisation worked fine. One slight issue here is that, having put a URL in this property in the GUI, I couldn't set it back to empty (null) .....
webview2_source.jpg
webview2_source.jpg (32.72 KiB) Viewed 441 times
...... in the end, I had to remove the control and re-insert a new one to get rid of the URL.
Last edited by mark106 on Tue Sep 10, 2024 1:34 pm, edited 1 time in total.
User avatar
brittneyr
Site Admin
Posts: 1799
Last visit: Fri Nov 01, 2024 10:29 am
Answers: 44
Been upvoted: 34 times

Re: Webview2

Post by brittneyr »

You should not be able to set the source from the Designer. If you are seeing the 'Property value is not valid' message, the value was not set. Selecting cancel will set it back to the last valid input which in this case would be back to empty. There is no way to input this data type into via the Designer in PowerShell Studio. This can only be set via code.

Can you provide an image of the 'corewebview2 in use' error message you received?
Brittney
SAPIEN Technologies, Inc.
User avatar
mark106
Posts: 32
Last visit: Wed Oct 09, 2024 5:36 am
Has voted: 1 time

Re: Webview2

Post by mark106 »

Hi,

PSS lets me set a source (URL) in the designer. In the screenshot above, the source property actually contains a valid URL - which I'm trying to remove, but the property will only accept valid URLs and not a "blank" value - so it's effectively stuck there.

If there is a source URL set in the designer, then this is the error I see ......
webview error.jpg
webview error.jpg (129.99 KiB) Viewed 414 times

However, if the source is only set in code, then it all works fine :D


Code: Select all

ERROR: Exception setting "CreationProperties": "CreationProperties cannot be modified after the initialization of CoreWebView2 has begun."
MainForm.psf (108, 2): ERROR: At Line: 108 char: 2
ERROR: +         $WebViewControl.CreationProperties = [Microsoft.Web.WebView2. ...
ERROR: +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
ERROR:     + FullyQualifiedErrorId : ExceptionWhenSetting
ERROR:  
ERROR: The property 'UserDataFolder' cannot be found on this object. Verify that the property exists and can be set.
MainForm.psf (124, 2): ERROR: At Line: 124 char: 2
ERROR: +         $WebViewControl.CreationProperties.UserDataFolder = $UserData ...
ERROR: +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR:     + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
ERROR:     + FullyQualifiedErrorId : PropertyNotFound
ERROR:  
User avatar
brittneyr
Site Admin
Posts: 1799
Last visit: Fri Nov 01, 2024 10:29 am
Answers: 44
Been upvoted: 34 times

Re: Webview2

Post by brittneyr »

You can remove the value by right-clicking on the property in the Properties panel and selecting 'Reset'.

The issue is with the initialization with the CreationProperties. In C#, the Source property can be set and you do not need to manually initialization the CreationProperties and set the UserDataFolder for the control to load. For some reason, the loading of the WebView2 control does not act the same in PowerShell. We will look into a possible resolution for that, but for now, I recommend only setting the Source property in code.
Brittney
SAPIEN Technologies, Inc.
User avatar
mark106
Posts: 32
Last visit: Wed Oct 09, 2024 5:36 am
Has voted: 1 time

Re: Webview2

Post by mark106 »

Thanks

I don't normally set the URI in the designer, I just happened to do it while testing the new WebView2 Control in an existing project - and then got caught out with that error.
You can remove the value by right-clicking on the property in the Properties panel and selecting 'Reset'.
Ahh! That's handy - I hadn't spotted that! Thanks

In general, "migrating" my existing project from WebBrowser to WebView2 has been more straightforward than I was expecting and the performance and responsiveness benefits have been really positive.
User avatar
brittneyr
Site Admin
Posts: 1799
Last visit: Fri Nov 01, 2024 10:29 am
Answers: 44
Been upvoted: 34 times

Re: Webview2

Post by brittneyr »

Please try service build 5.8.249 and let me know if you are still having any issues with the WebView2 control in the designer.
Brittney
SAPIEN Technologies, Inc.
This topic is 1 month and 4 days old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.