2016-07-28 90 views
0

我在我的VB.NET(Windows Form App)程序中使用GeckoWebBrowserGeckoWebBrowser加载本地文件html。该html嵌入了内嵌svg文件(human body diagram with bones and internal organs)与javascript函数,用于从svg文档中提取元素的所有“ID”。我想从VB.NET(Windows窗体应用程序)调用前述javascript函数,但我不知道如何去做。任何人都可以帮助我,或者请给我一个源代码示例吗?所有我发现的东西是基于在C# ... 这是我javascript功能在我html文件:VB.NET Gecko Web浏览器JavaScript函数调用?

<script type="text/javascript"> 

(funcion() { 

// Function to be called in VB.NET when the DOM is loaded 


var SVGHandler = function() { 

    // Picking up the id Root Node="CUERPO_HUMANO" into svg variable 

    var svg = document.querySelector('#CUERPO_HUMANO'); 


    // In Items we save all the <g> which have an ID 

    var items = svg.querySelectorAll('g[id], path[id]'); 

    //var items = svg.querySelectorAll('g[id]'); 

    // We loop all the nodes saved in Items and add them to click event listener 

    forEach(items, function (index, value) { 

     value.addEventListener('click', function (event) { 



       event.preventDefault(); 

       //We avoid the spread of events 
       event.stopPropagation(); 





       return event.currentTarget.id 

       // console.log(event.currentTarget.id) 

      }); 

    }); 



} 

// https://toddmotto.com/ditch-the-array-foreach-call-nodelist-hack/ 

var forEach = function (array, callback, scope) { 

    for (var i = 0; i < array.length; i++) { 

    callback.call(scope, i, array[i]); // passes back stuff we need 

    } 

}; 



// With this method, we call a SVGHandler when DOM is totally loaded 

document.addEventListener('DOMContentLoaded', SVGHandler); 

})(); 

</script> 

什么代码,我应该在VB.NET使用,每次我点击一个打电话给我的javascript功能GeckoWebBrowser加载的人体图中特定的骨或器官? 我想将通过调用获取的“id”保存为string变量,以便将其用作SQL语句中的参数并填充DataGridView。 我一直在寻找,所有我能找到的与C#有关,而不是一个VB.NET的例子。尽管我试图找出VB.NET尝试将C#的示例转换为VB.NET的等价性,但我对如何执行javascript调用存在一些疑问。据我javascript功能它可能是这样的:

browserControl.Navigate("javascript:void(funcion())"); 

请,任何人都可以帮我解决这个问题?我会非常感谢...

回答

1

那么既然你已经设置点击EventListener的我认为你不是在寻找一种方式来调用VB.NET最终的功能,但这是相当不清楚根据您的文章,所以我我将给你举例说明如何使用GeckoWebBrowser通过javascript调用javascript函数以及如何触发VB.NET代码中的反应。

您的代码片段试图从您的vb代码调用js函数是正确的。唯一的问题是你没有在你的html文件中定义任何可调用的js函数。在你的情况,你应该这样做,从VB触发你的主js函数:

//Sorry I don't know vb. I'll give example in c# keeping it as simple as possible so that you can easily convert it to vb 

Gecko.GeckoHtmlElement humanBodyPart = (Gecko.GeckoHtmlElement) browserControl.Document.GetElementById("your id"); 
humanBodyPart.Click(); 

上面的代码会查找元素具有匹配id在GeckoWebBrowser并点击它。既然你已经设置了点击EventListener's,通过点击其中一个元素,这将触发分配给它们的功能来运行。

继续前进,为了将元素的ID保存到vb代码中的string变量中,您需要将这一点js代码添加到您作为“回调”参数传递给您的代码中forEach功能:

var event = document.createEvent('MessageEvent'); 
var origin = window.location.protocol + '//' + window.location.host; 
var event = new MessageEvent('jsCall', { 'view': window, 'bubbles': false, 'cancelable': false, 'data': 'YOUR EVENTUAL ID AS A STRING (THIS STUFF GOES BACK TO THE VB/C# CODE)' }); 
document.dispatchEvent (event); 

接着上面的代码段应该在您的VB代码,这样的处理方式:

browserControl.AddMessageEventListener("jsCall", (id) => 
{ 
    //Here in the variable id you have your clicked id as a string. Do what you wanted to do... 
}); 
0

VB的一面: 你需要等到文件完成增加听众 例如:_DocumentCompleted

Private Sub GeckoWebBrowser1_DocumentCompleted(sender As Object, e As Gecko.Events.GeckoDocumentCompletedEventArgs) Handles GeckoWebBrowser1.DocumentCompleted GeckoWebBrowser1.AddMessageEventListener("my_function_name JS_side", AddressOf my_sub_for_treatment) End Sub

JS侧:

var event = document.createEvent('MessageEvent'); 
 
var origin = window.location.protocol + '//' + window.location.host; 
 
var event = new MessageEvent('my_function_name JS_side', { 'view': window, 'bubbles': false, 'cancelable': false, 'data': my_data_to transfer }); 
 
document.dispatchEvent (event);