Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC:

WebView2 on X# VOForm: empty when using CustomControl 14 Mar 2022 22:49 #21930

  • ic2
  • ic2's Avatar
  • Topic Author


  • Posts: 1608
  • Earlier I asked about applying the WebView2 in X#. On www.xsharp.eu/forum/public-product/2902-...oform?start=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
    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
    Using Microsoft.Web.WebView2.CorePartial Class Window1 Inherit DATAWINDOW
    
    Protect webView2 As Microsoft.Web.WebView2.WinForms.WebView2
    Protect oDCPICT As CUSTOMCONTROL
    Self:webView2 := Microsoft.Web.WebView2.WinForms.WebView2{}
    Self:webView2:Visible:=True
    cHTMLPath:="c:\temp\SomeFile.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:
    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())

    Please Log in or Create an account to join the conversation.

    Last edit: by ic2.

    WebView2 on X# VOForm: empty when using CustomControl 15 Mar 2022 00:41 #21933

    • Chris
    • Chris's Avatar


  • Posts: 3843
  • 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?

    .
    XSharp Development Team
    chris(at)xsharp.eu

    Please Log in or Create an account to join the conversation.

    WebView2 on X# VOForm: empty when using CustomControl 15 Mar 2022 12:55 #21938

    • ic2
    • ic2's Avatar
    • Topic Author


  • Posts: 1608
  • 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 )
    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

    Please Log in or Create an account to join the conversation.

    Last edit: by ic2.

    WebView2 on X# VOForm: empty when using CustomControl 15 Mar 2022 16:49 #21942

    • robert
    • robert's Avatar


  • Posts: 3446
  • Dick,

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

    Robert
    XSharp Development Team
    The Netherlands

    Please Log in or Create an account to join the conversation.

    • Page:
    • 1