简体   繁体   English

如何使用响应从C#调用运行网页的CEFSharp

[英]How to call from C# to CEFSharp that run a web page using react

I have created ac# app that embed CEFSharp. 我创建了一个嵌入CEFSharp的ac#应用。 From C#, I can call a javascript function using cefsharp.ExecuteScriptAsync. 从C#中,我可以使用cefsharp.ExecuteScriptAsync调用javascript函数。 eg in javsscript, I have a function showAlertFromClient(msg) from c#, I can call this Javascript function using 例如在javsscript中,我有一个来自c#的函数showAlertFromClient(msg) ,我可以使用

cefsharp.ExecuteScriptAsync("showAlertFromClient('Hi from C#');");

However, the web page is done using React and I have no idea on how to expose a function in react where I can call it from c#. 但是,该网页是使用React完成的,我不知道如何在react中公开可以从c#调用它的函数。

eg of react, index.js import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; ReactDOM.render( <App/>, document.getElementById('root')); 例如react,index.js import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; ReactDOM.render( <App/>, document.getElementById('root')); import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; ReactDOM.render( <App/>, document.getElementById('root'));
So do I expose a JS function eg showAlertFromClient() in React where I can call if from c#? 那么我showAlertFromClient()在React中公开一个JS函数,例如showAlertFromClient() ,如果我从c#可以调用它呢? Anyone have any idea how to achieve that? 有人知道如何实现吗? Thanks 谢谢

Create a new.js file and add your method(showAlertFromClient) in this file. 创建一个new.js文件,然后在该文件中添加您的方法(showAlertFromClient)。 Call this function like 像这样调用此函数

cefsharp.ExecuteScriptAsync("showAlertFromClient('Hi from C#');");

Include this file in head of index.html. 将此文件包含在index.html的头部。

inside your method use window.anyname('anything'.): 在方法内部使用window.anyname('anything'。):

showAlertFromClient('Hi from C#'){
   window.updateUi('Hi from C#');

} }

In component of react.js 在react.js的组件中

componentWillMount() {
window.updateUi= (data) => {
  this.setState({
    newData: data
  });
}

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

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