Problems with WebView2 distribution

Plan_OK

Member
Joined
Aug 21, 2023
Messages
24
Programming Experience
1-3
Visual Studio 2022 Community Edition
Development tools for .NET Framework 4.7.2
Visual Basic

Hello,
I have been looking for similar problems with little result,
I have inserted a webview2 control that works fine with the Visual Studio interface, when I create the deployment program the control is no longer visible and therefore neither is the web link (even on the same computer).
I added (manually the webview2loader.dll library, but not webview2loaderruntime.dll because I can't find it anywhere on the whole computer.
I think the problem lies here or is something else still missing? (what do you think?). But how to locate the library location or is it necessary to use a different approach?
Thank you.
 
Sure, sorry, I thought of it right after I posted.
The problem is that everything works before the Await call and immediately after ...
I'll try to explain, it would be faster to see the effects (I'll send you the link with the project presentation from which you can see the appearance and functionality and better understand what happens after the Await call). I have become convinced that when the Await command arrives, the flow stops and all subsequent commands have no way to be executed.
I am only telling you two things to try to make myself better understood and because I feel like I am committing too much time to you.

The link is:
Affari Nostri - Software Vic 2023 - vb.net

in this space I have also included, at the end of the presentation, screenshots related to the problem (I don't want to take up too much forum space) I hope you will look at them. I ask you to also take a look at the error message when compiling.

- On p. 6 you can see that, for example, the board is filled with the appropriate values that disappear (become 0) as soon as you start the program after the call.

Statement of some elements::
    Dim Elemento() As Integer = {0, 1, 5, 10, 20, 50, 75, 100, 200, 500, 5000, 10000, 15000, 20000, 30000, 50000, 75000, 100000, 200000, 300000}
    Dim ElemTabella(20) As Integer ' deposito di servizio
    Dim CronoPacchi(20) As Integer ' Pacchi del Tabellone
    Dim CronoValore(20) As Integer ' Valore del Tabellone
    Dim CronoPacchiMiei(20) As Integer ' I pacco e Pacchi scelti - utilizzati solo i primi 10
    Dim XPosEticReg() As Integer = {0, 71, 142, 213, 284, 355, 426, 0, 71, 142, 213, 284, 355, 426, 0, 71, 142, 213, 284, 355, 426}
    Dim YPosEticReg() As Integer = {50, 50, 50, 50, 50, 50, 50, 108, 108, 108, 108, 108, 108, 108, 166, 166, 166, 166, 166, 166, 166}

In Form1_load they are initialized:
VB.NET:
        For i = 0 To 19
            ElemTabella(i) = Elemento(i)
            CronoPacchiMiei(i) = i
            CronoValore(i) = Elemento(i)
            CronoPacchi(i) = i
        Next

and fill in the relevant labels:
VB.NET:
    Sub InizializzaTabellone()
        For i = 1 To 20
            For Each C As Control In grpTabella.Controls.OfType(Of Label)()
                If C.Name = "lblTabella" & i Then
                    C.Text = Format(Elemento(i - 1), "##,##0.")
                    C.Visible = True
                End If
            Next
        Next
    End Sub


- The home page of the program shows elements covered by GroupBox containers that disappear as soon as choices are made. These (all of them) are not visible, so the elements below them remain uncovered immediately.

- All active buttons have no action; only the WebView2 control is working: the impression is that activating this control, blocks any other command in the form_load.

I know I am asking a lot, but I also know that an experienced eye needs a limited amount of time to detect what is wrong. I also know that the information I give you may not be complete and adequate, understand me.
Thank you, Vic.
 
1694694823750.png


The event handler must be a Sub. "Function ... As Task" is wrong. If you change Function to Sub and remove "As Task" it should be ok again.
 
Thank you for the speed. I made the requested changes, but the situation is not resolved, in fact maybe a little worse. I keep getting the message:
System.Reflection.TargetInvocationException:
.
.
.
Plus now after startup the system crashes and I need to stop from "Stop Debug".

I tried to go to the working test project and saw that the form_loard Sub has been changed (from the "Quick Actions" suggestion) to Function and everything works normally.

Here is a section of the listing:
VB.NET:
    Private Async Function Form1_LoadAsync(sender As Object, e As EventArgs) As Task Handles MyBase.Load
        Await InitializeWebViewEnvironment(WebView21)
        cmdVai_Click(Me, EventArgs.Empty)
    End Function

    Public Async Function InitializeWebViewEnvironment(browser As Microsoft.Web.WebView2.WinForms.WebView2) As Task
        Dim userDataFolder = Path.Combine(Application.LocalUserAppDataPath, "WebView2")
        Dim env = Await CoreWebView2Environment.CreateAsync(Nothing, userDataFolder, Nothing)
        Await browser.EnsureCoreWebView2Async(env)
    End Function
I don't know what to do.
 
It doesn't look like you fixed it to me. Did you read the previous post?
This is how the event handler signature must be:
VB.NET:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
In addition you must add Async to use the Await in the method:
VB.NET:
Private Async Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
 
JohnH, I see that you have great trust in others and high regard. However, this time I think it is you who did not read the previous post(s) properly:
With the changes you suggested, the program crashes!: I hope you can guess in the background the output of the program launch: the controls are hinted at and the flow is blocked. Perhaps there is another impediment, but I, who can read little, do not know what it is.
The "Working" reference was related to the test I had done starting from an empty project (see previous posts) in which with the code shown above, the program works and allows the implementation of the installer (which is the thing I am missing).
In the attached file I send you a screenshot with many elements juxtaposed. In the background, behind the windows, you can see the output of the program you just ran. If you need more to convince you that this is the case, tell me what you need (would a TeamViewer link suffice?).
 

Attachments

  • errore32.jpg
    errore32.jpg
    455.6 KB · Views: 7
This stands out:
1694720391855.png

This will happen if Source is set in designer, because then WebView2 will initialize the environment and begin navigation before Load event. Reset this property in Designer and after environment is initialized in Load event set the Source in code.
VB.NET:
Await InitializeWebViewEnvironment(browser)
browser.Source = new Uri("http://address")
 
Thanks for the analysis, but there is something else preventing WebView2 from initializing correctly, in fact the Source property is not set in designer, but only in code. I tried deleting and inserting the control again, but the result is the same.

I have, however inserted the line of code:
VB.NET:
    Private Async Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Await InitializeWebViewEnvironment(WebView21)
        WebView21.Source = New Uri("https://www.raiplay.it/dirette")
But I keep getting the message:
ArgumentException: WebView2 was already initialized with a different CoreWebView2Environment. Check to see if the Source property was already set or EnsureCoreWebView2Async was previously called with different values.

Could there be another cause other than the definition of WebView2.Source in designer or code?
 
1. Take a look in the forms .designer.vb file in InitializeComponents see if Me.WebView21.Source is set still.
1694760670500.png

1694760708753.png
< no Me.WebView21.Source here

2. You haven't added a Sub New and set Source there?
1694762624289.png
< No.

3. You don't have a duplicate Load event handler like below as leftover after messing with Intellisense suggestions?
1694762697624.png
 
Last edited:
In the main form I have only one WebView2 component, I have another form where there is a WebView2 control, but even there the Source property is not set.
The only routine where the Source property is found is in the following:
VB.NET:
    Private Sub txtURL_KeyDown(sender As Object, e As KeyEventArgs) Handles txtUrl.KeyDown
        If e.KeyCode = Keys.Enter Then
            If modoTV = "rai1" Then
                WebView21.Source = New Uri(txtUrl.Text)
            ElseIf modoTV = "raiPlay" Then
                cmdVai.Visible = True
                txtUrl.Visible = True
                txtUrl.Text = "https://www.raiplay.it"
                WebView21.Source = New Uri(txtUrl.Text)
            ElseIf modoTV = "vic" Then
                '                WebView21.Anchor = "Top, Bottom, Left, Right"
                MaximizeBox = True
                cmdVai.Visible = True
                txtUrl.Visible = True
                txtUrl.Text = "https://www.vicariweb.it"
                WebView21.Source = New Uri(txtUrl.Text)
            End If
        End If
    End Sub
I attach the screenshot of the file
(I never know if I can use multiple screenshots in these cases: I don't want to use too much space)

webview2a.jpg

Given your request I will answer that I have a Sub New() that initializes component and that is used to pass a variable to another module, I don't know if this causes the error.
VB.NET:
    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub lblVic_Click(sender As Object, e As EventArgs) Handles lblVic.Click
        If chkBrowser.Checked = True Then
            VaiWeb("https://www.vicariweb.it")
        Else
            Dim frm As New tvRAI1("vic")
            frm.Show()
        End If
    End Sub

If so, what to do?
 
The textbox can't receive a KeyDown before the form is loaded and shown, so it can't be that.
 
Hi, I tried to analyze the listing and found these lines that are certainly responsible for the error (even though they are not called initially).
In fact, excluding them from the code and running the program no longer causes the blockage and the flow proceeds normally.
VB.NET:
 Private Sub cmdVai_Click(sender As Object, e As EventArgs) Handles cmdVai.Click
        Navigate(txtUrl.Text)
    End Sub

    Private Sub Navigate(ByVal address As String)
        If String.IsNullOrEmpty(address) Then Return
        If address.Equals("about:blank") Then Return

        If Not address.StartsWith("http://") AndAlso Not address.StartsWith("https://") Then
            address = "http://" & address
        End If

        Try
            WebView21.Source = (New Uri(address))
        Catch __unusedUriFormatException1__ As System.UriFormatException
            Return
        End Try
    End Sub
Can someone tell me if it is normal that WebView2.Source is involved before form_load and why?
 
Can you reproduce this problem in a new project with a WebView2 and a button?
 
It was not necessary to test on a new project (done previously and it was working) because I think I found the error.
The Navigate() routine works correctly and after numerous tests and modifications I found a call to the Navigate() routine from within a CheckedChanged event, ...
VB.NET:
     Private Sub chkTV_CheckedChanged(sender As Object, e As EventArgs) Handles chkTV.CheckedChanged
        If chkTV.Checked = True Then
            WebView21.Visible = True
            WebView21.Enabled = True
            cmdMeno.Visible = True
            cmdPiu.Visible = True
            txtUrl.Visible = True
            cmdVai.Visible = True
            cmdTVlarge.Visible = True
            cmdIndietro.Visible = True
            cmdAvanti.Visible = True
            WebView21.Size = New Size(450, 254)
            cmdMeno_Click(Me, EventArgs.Empty)
            If TVlarge Then grpGioca.Visible = False
        Else
            WebView21.Visible = False
            WebView21.Enabled = False
            cmdMeno.Visible = False
            cmdPiu.Visible = False
            txtUrl.Visible = False
            cmdVai.Visible = False
            cmdTVlarge.Visible = False
            cmdIndietro.Visible = False
            cmdAvanti.Visible = False
            grpGioca.Visible = True
        End If
        Navigate("https://www.raiplay.it/dirette/rai1")
    End Sub
Removed this call and the error disappears. Replaced the call with the address in txtUrl.Text to be handled by the cmdVai button that will later call Navigate().
VB.NET:
            grpGioca.Visible = True
        End If
        txtUrl.Text = "https://www.raiplay.it/dirette/rai1"
    End Sub
The question still remains: is the event processed before the form_load?
 
Yes, CheckedChanged happen immediately for handled checkboxes if InitializeComponents (that is called from contructor) sets the initial checkbox Checked state to True (which is a change from default False).
 
Back
Top