xsharp.eu • WebView2 on X# VOForm: empty when using CustomControl
Page 1 of 1

WebView2 on X# VOForm: empty when using CustomControl

Posted: Mon Mar 14, 2022 9:49 pm
by ic2
Earlier I asked about applying the WebView2 in X#. On https://www.xsharp.eu/forum/public-prod ... t=20#21737, where Frank Müßner posted an example how to add a Winforms PictureBox on an X# VO form. With a slight modification in SetParent the fully prepared PictureBox could be assigned to a VO CustomerControl

Code: Select all

SetParent(Self:pictureBox1:Handle, Self:oDCPict:Handle())

So I thought I do about the same to add a WebView2 control to a VO form. I installed the 3 necessary DLL's via a NuGet package, used the same CustumControl odcPict on the VO form and used this code in the PostInit. But the control remains empty, both when I use an existing html file or a valid URI (now outcommented).

What do I miss? It works with a few lines of code on an X# Winforms program or a WPF program.

EDIT: it does work when I directly assign the content to the window (like in Frank's oroginal sample). But why doesn't it work in a X# CustomControl?

See listing 2

Dick

Code: Select all

Using Microsoft.Web.WebView2.CorePartial Class Window1 Inherit DATAWINDOW

Protect webView2 As Microsoft.Web.WebView2.WinForms.WebView2
Protect oDCPICT As CUSTOMCONTROL

Code: Select all

Self:webView2 := Microsoft.Web.WebView2.WinForms.WebView2{}
Self:webView2:Visible:=True
cHTMLPath:="c:tempSomeFile.html"
Self:webView2:Source := System.Uri{cHTMLPath, System.UriKind.Absolute}
//Self:webView2:Source := System.Uri{"https://www.xsharp.eu", System.UriKind.Absolute}
Self:webView2:Update()

SetParent(Self:webViewEmail:Handle, Self:oDCPict:Handle())		
Listing2 : it works when I do not use the customcontrol: and use Self:Handle again in SetParent:

Code: Select all

Self:webViewEmail := Microsoft.Web.WebView2.WinForms.WebView2{}
Self:webViewEmail:Visible:=True
Self:webViewEmail:Location := System.Drawing.Point{1, 1}
Self:webViewEmail:Size := System.Drawing.Size{800, 800}
Self:webViewEmail:Source := System.Uri{"https://www.xsharp.eu", System.UriKind.Absolute}
Self:webViewEmail:Update()
SetParent(Self:webViewEmail:Handle, Self:Handle())

WebView2 on X# VOForm: empty when using CustomControl

Posted: Mon Mar 14, 2022 11:41 pm
by Chris
Hi Dick,

This should work I think, hard to tell why it doesn't. Can you zip and post the test solution so we can have a look?

.

WebView2 on X# VOForm: empty when using CustomControl

Posted: Tue Mar 15, 2022 11:55 am
by ic2
Hello Chris,

I was preparing the solution for you, added some comment to these lines below, executed the "direct write" version which worked and then the custom control version, which did not....and now it did!

I really didn't change anything else and I tried a dozen different version yesterday, varying all kind of properties without result. The control remained empty. And now it works. Also after a restart. No idea why it didn't yesterday.

I'll stop amazing myself and good it works (and I hope it keeps working :P )

Code: Select all

SetParent(Self:webViewEmail:Handle, Self:oDCPict:Handle())								// This does not work, uses the CustomControl
//SetParent(Self:webViewEmail:Handle, Self:Handle())												// This works, display WebView2 content directly on the window

Dick

WebView2 on X# VOForm: empty when using CustomControl

Posted: Tue Mar 15, 2022 3:49 pm
by robert
Dick,

I would recommend to set the parent first and then set the source and call Update()

Robert