我怎么能一个Windows窗体应用程序和WebBrowser控件之间进行通信 [英] How can I communicate between a windows forms application and a WebBrowser control

查看:432
本文介绍了我怎么能一个Windows窗体应用程序和WebBrowser控件之间进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法,我可以从包含窗口将数据推送到WebBrowser控件任何机构形成的应用程序。具体来说WebBrowser控件包含一些knockout.js,我想允许一些外部事件,比如本地文件系统更改某些文件以更新淘汰赛视图模型。

Is there any mechanism that I can push data into a webbrowser control from the containing windows forms application. 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控件发出longpolling JSONP请求到微web服务器我已经写在窗口中运行的表单应用程序与JS时尚后(注意,这是不是网页的地方本身是服务器从这里JSONP。此外,微Web服务器基本上是一个httpListner)。当窗体应用程序接收一个文件更改事件返回通过微型网络服务器的响应和更新淘汰赛视图模型。

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). When the forms app receives a file change event it returns a response via the micro webserver and that updates the knockout viewmodel.

问题是这一切感到脆弱 - 如果文件中的事件来厚,快速,他们可以开始被跳过 - 它看起来像我的微Web服务器正在试图返回多个结果,以相同的流。我可以解决这个问题,但总体看它讨厌的和脆弱的。

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.

问题是:(!不需要刷新页面)我错过任何更简单的方式有包含应用程序的信号东西(即通过一些JSON),以在webbrowsercontrol网页运行

The question is: am I missing any much simpler way to have the containing application signal something (i.e. pass some JSON) to the web page running in a webbrowsercontrol (without a page refresh!)

推荐答案

您可以创建一个元素,比如用一个id的div

You could create an element, say a div with an id

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

然后从您的WinForms中的应用程序。

Then write json to this div from within your winforms app..

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.

这篇关于我怎么能一个Windows窗体应用程序和WebBrowser控件之间进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆