1 回答

TA貢獻(xiàn)2016條經(jīng)驗(yàn) 獲得超9個(gè)贊
您的代碼中的問題: 您沒有正確注冊 Js 對象,這就是您無法在 JS 中獲取對象的原因。
準(zhǔn)則:
RegisterJsObject 是注冊你的c#對象,然后從JS調(diào)用這些方法,并將值從JS發(fā)送到c#。
如果你想將空字符串從 c# 傳遞到你的 HTML 頁面,那么你應(yīng)該像下面這樣注冊 JS 對象:
您的 C# 類應(yīng)如下所示:
public class AsyncBoundObject
{
//We expect an exception here, so tell VS to ignore
[DebuggerHidden]
public void Error()
{
throw new Exception("This is an exception coming from C#");
}
//We expect an exception here, so tell VS to ignore
[DebuggerHidden]
public int Div(int divident, int divisor)
{
return divident / divisor;
}
}
然后你可以在 CefSharp 實(shí)例中注冊這個(gè)類,如下所示:
browser = new ChromiumWebBrowser();
browser.RegisterAsyncJsObject("boundAsync", new AsyncBoundObject());
注冊后,您可以從 JS 調(diào)用該方法,如下所示。
function asyncDivOk()
{
var call = "Async call (Divide 16 / 2): " + Date();
window.boundAsync.div(16, 2).then(function (res)
{
var end = "Result: " + res + "(" + Date() + ")";
writeAsyncResult(call, end);
});
}
您可以看到 16 和 2 是正在傳遞的參數(shù)。
希望這可以幫助。
- 1 回答
- 0 關(guān)注
- 357 瀏覽
添加回答
舉報(bào)