【发布时间】:2021-07-27 11:33:47
【问题描述】:
将消息从 Typescript 发布到 C# 的正确方法是什么?我正在尝试使用 WebView_WebMessageReceived 使用以下代码在 C# 中接收消息:
private void WebView_WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs e)
{
var json = e.TryGetWebMessageAsString();
}
我在打字稿文件中尝试了以下内容:
window.chrome.webview.postMessage("test");
window.postMessage("test", '*');
window.postMessage({type: 'hello', payload: {}}, '*');
但是在 C# 中没有收到消息。上面的代码在纯 html 而不是 typescript 中工作。请提出建议..
【问题讨论】:
-
向 C# 发布消息是什么意思?你的意思是调用 API 还是什么?
-
@MohamadMousheimish 我在 Windows Form C# 中使用 WebView2 来加载 html(包括打字稿)。 WebView2 中有 WebMessageReceived 事件,当从 html 发布消息时,该事件应接收消息,如此链接中包含的:weblog.west-wind.com/posts/2021/Jan/26/…。使用 JavaScript 在纯 html 中一切正常:window.chrome.webview.postMessage('Hello .NET from JavaScript');但同样的事情不适用于打字稿。
标签: c# typescript postmessage