简体   繁体   English

如何在Windows窗体应用程序和WebBrowser控件之间进行通信

[英]How can I communicate between a windows forms application and a WebBrowser control

Is there any mechanism that I can push data into a webbrowser control from the containing windows forms application. 有什么机制可以将数据从包含Windows窗体的应用程序推送到Web浏览器控件中。 Specifically the webbrowser control contains some knockout.js and I want to allow some external events such as changes to certain files in the local filesystem to update the knockout viewModel. 具体来说,webbrowser控件包含一些kickout.js,并且我希望允许一些外部事件(例如,对本地文件系统中某些文件的更改)来更新淘汰模型viewModel。

At the moment I have this working after a fashion with the JS in the webbrowser control issuing a longpolling jsonp requests to a micro webserver I have written running in the windows forms application (note that this is not the place that the web page itself is server from hence jsonp. Also, the micro webserver is basically just a httpListner). 目前,我正在通过webbrowser控件中的JS进行某种方式的工作,向我编写的在Windows窗体应用程序中运行的微型Web服务器发出longpolling jsonp请求(请注意,这并不是网页本身是服务器的地方从jsonp开始。此外,微型网络服务器基本上只是一个httpListner)。 When the forms app receives a file change event it returns a response via the micro webserver and that updates the knockout viewmodel. 表单应用程序收到文件更改事件时,它将通过微型网络服务器返回响应,并更新基因剔除视图模型。

The problem is this all feels fragile - if the file events come thick and fast they can start being skipped - it looks like my micro webserver is trying to return multiple results to the same stream. 问题是这一切都很脆弱-如果文件事件变得又快又厚,可以开始跳过它们-看来我的微型网络服务器正在尝试将多个结果返回到同一流。 I can fix that, but overall its looking nasty and fragile. 我可以解决此问题,但总体而言,它看起来令人讨厌且脆弱。

The question is: am I missing any much simpler way to have the containing application signal something (ie pass some JSON) to the web page running in a webbrowsercontrol (without a page refresh!) 问题是:我是否缺少任何一种更简单的方法来使包含的应用程序向运行在webbrowsercontrol中的网页发出某种信号(即,传递一些JSON)(无需刷新页面!)

You could create an element, say a div with an id 您可以创建一个元素,例如一个带有id的div

<div id='external-json' data-bind='jsonHandler : {}' style='display:none'></div>

Then write json to this div from within your winforms app.. 然后从您的winforms应用程序内将json写入此div。

var myJson = "{test : 1}";
this.browser.Document.Body.GetElementById("external-json").innerText = myJson; 

You could even create a knockout custom binding to react to this 您甚至可以创建一个敲除自定义绑定以对此做出反应

    ko.bindingHandlers.jsonHandler = {
        update: function(element, valueAccessor) {
        //do something with the json
        var json = element.innerText;
        var obj = eval(json); //hmm
        alert(obj.test); 
    }
};

This is obviously a simple example and you could probably improve the idea a lot, you may need a queue of messages, possibly calling a function in your client script instead of using the element. 这显然是一个简单的示例,您可能会大大改进此想法,可能需要一堆消息,可能在客户端脚本中调用一个函数而不是使用元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WebBrowser控件和Windows窗体之间的交互 - Interaction between WebBrowser Control and Windows Forms Windows Forms应用程序中,如何在自定义控件之间进行通信? - Windows Forms application, how to communicate between custom controls? 如何在Windows Forms WebBrowser控件中拦截刷新? - How to intercept refresh in Windows Forms WebBrowser control? 如何在正在运行的Forms应用程序中动态更改WebBrowser对象? - How can I change a WebBrowser object dynamically in a running Forms application? Windows窗体WebBrowser控件和iframe - Windows Forms WebBrowser control and iframes Windows窗体WebBrowser控件和AJAX - Windows Forms WebBrowser Control and AJAX 我如何在C#Windows窗体应用程序中的Webbrowser控件中显示存储的html代码,而不在硬盘上创建html文件? - How can i display stored html codes in webbrowser control in C# windows form application without creating html files on hard drive? 如何从Windows窗体Webbrowser控件中删除元素 - How to remove elements from a windows forms webbrowser control 如何在Windows Forms WebBrowser控件中的页面上调用Click事件? - How to call the Click event on a page in the Windows Forms WebBrowser control? How can i fix Application&#39; 是 &#39;System.Windows.Application&#39; 和 &#39;System.Windows.Forms.Application&#39; 之间的模棱两可的引用 - How can i fix Application' is an ambiguous reference between 'System.Windows.Application' and 'System.Windows.Forms.Application'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM