简体   繁体   English

通过在 HTML 页面上单击的按钮呼叫 C# function

[英]Call a C# function from a button clicked on an HTML page

My application (C#) consists of an embedded web browser which will load a page in HTML from a server.我的应用程序 (C#) 包含一个嵌入式 web 浏览器,它将从服务器加载 HTML 中的页面。 The HTML page will be created from scratch for this particular application.将为此特定应用程序从头开始创建 HTML 页面。 My intention is to have the following procedure:我的意图是采用以下程序:

  • A user opens the application用户打开应用程序
  • A web browser embedded within the application will load an HTML page应用程序中嵌入的 web 浏览器将加载 HTML 页面
  • User will click a button on the HTML page用户将单击 HTML 页面上的按钮
  • A C# function (on client side) is triggered from this action从此操作触发 C# function(在客户端)
  • function does some work function 做了一些工作
  • send a list of data back to the web page将数据列表发送回 web 页面
  • Web page will retrieve this data and display it Web 页面将检索此数据并显示
  • user's browser will be refreshed to display the new page用户的浏览器将被刷新以显示新页面

Now my questions are the following:现在我的问题如下:

  • How can a C# function on the client side be triggered when a button is clicked on a server page如何在服务器页面上单击按钮时触发客户端的 C# function

  • What is the best way to transfer data (will probably be object)传输数据的最佳方式是什么(可能是对象)

  • how can a function on the HTML page wait for data to be received before continuing to execute the function? HTML页面上的一个function怎么会等到接收到数据再继续执行function呢?

Using the WebBrowser control, you can use the ObjectForScripting property.使用 WebBrowser 控件,您可以使用ObjectForScripting属性。 This will allow JavaScript to call C# code using window.external.这将允许 JavaScript 使用 window.external 调用 C# 代码。 C# MethodName C# 方法名

There's some quirks with the serialization you might need to work through, and you might need to use webbrowser .Document.InvokeScript to trigger your data back to JavaScript (I've not personally used the result of a window.external call so cannot speak for it).您可能需要完成序列化的一些怪癖,您可能需要使用webbrowser .Document.InvokeScript 将数据触发回 JavaScript(我个人没有使用 window.external 调用的结果,所以不能代表它)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM