Buongiorno a tutti. Premetto che sono nuovo della programmazione VB.NET, quindi chiedo scusa in anticipo per le castronerie che potrei dire.
Sto cercando di costruire un WindowsApplication in VB.NET che carichi una pagina html all'ilnterno di un componente WebBrowser e che richiami, a caricamento della pagina avvenuto, un JavaScript contenuto nella pagina html stessa.
Per fare questo ho prima di tutto aggiunto il componente "Microsot Web Browser" al form ed ho richiamato il metodo "InvokeScript" all'interno di DocumentComplete:
codice:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AxWebBrowser1.Navigate("file://D:\WindowsApplication1\TestPage.html")
End Sub
Private Sub AxWebBrowser1_DocumentComplete(ByVal sender As Object, ByVal e As AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent) Handles AxWebBrowser1.DocumentComplete
Dim returnValue As Object
Dim ObjArr(1) As Object
ObjArr(0) = CObj(New String("Pippo"))
MessageBox.Show("Pagina completata!")
Try
returnValue = AxWebBrowser1.Document.InvokeScript("DisplayName", ObjArr)
Catch ex As System.Runtime.InteropServices.MarshalDirectiveException
MessageBox.Show("Impossibile eseguire lo script " & ex.Message)
Finally
MessageBox.Show("Fine!")
End Try
End Sub
il codice della pagina html è
codice:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Calling javascript</title>
<script type="text/javascript" language="javascript">
function DisplayName(name)
{
this.lbl1.document.writeln("Welcome " + name);
}
</script>
</head>
<body>
<div>
<label id="lbl1"></label>
</div>
</body>
</html>
Il problema è che la chiamata a InvokeScript fallisce generando un eccezione. Il MsgBox mi mostra "Limitazione del gestore di marshalling: stringa troppo lunga."
Cosa sto sbagliando????
Forse può esservi utile sapere che ho seguito come esempio quanto riportato qui:
http://www.dotnetcurry.com/ShowArticle.aspx?ID=194
Grazie mille in anticipo,
Nicola.