【问题标题】:JavaScript event handler/listener for C# eventC# 事件的 JavaScript 事件处理程序/侦听器
【发布时间】:2017-12-16 12:12:24
【问题描述】:

我正在用 JavaScript(UWP) 编写应用程序。我正在使用用 C# 编写的 Windows 运行时组件来处理我的应用程序的所有网络流量。我已经制作了事件,因此当我从服务器接收数据时,我会调用一个事件。

现在我想从 JavaScript 文件中监听这个事件,在 Visual Studio 中它看起来工作正常(参见图片链接 (1)),但是当我使用 node.js 编译时出现错误(参见图片链接 ( 2))。

 //Delegating
public delegate void AssistResponseDelegate(AssistResponse response);
 //Creating the event
public event AssistResponseDelegate AssistResponseReceived;
-------------------------------------------------------------------------
//In my JS file...

//Assigning a listener, n_manager being a class.
n_manager.assistresponsereceived = function (response) {            
      console.log("Listener called");                     //debug
      //call function to handle the response further

}

所以,奇怪的是在 Visual Studio 中调用和分配侦听器 When hoovering my mouse over the JS code for calling/assigning the listener it understands that it is c# code and seems to work 似乎可以正常工作,但是当我使用 Node.js 编译我的解决方案时,我收到一条错误消息,指出 the Network is not defined。所以我的问题是 node.js 无法读取 c# 命名空间?

【问题讨论】:

  • AssistResponseReceived?.Invoke(ar); 你确定这是"event" 吗?请上传更多相关代码sn-p。比如你声明事件的代码sn-p,关于AssistResponseReceivedAssistResponseDelegate的代码sn-p。
  • @SunteenWu-MSFT 感谢您的评论,我已经编辑了我的问题,尝试按照您的要求为您提供更多信息和代码。
  • 如何使用 node.js 编译您的解决方案?请提供minimal reproducible example 和重现步骤,以便我们进行测试

标签: javascript c# node.js uwp event-handling


【解决方案1】:

所以我想通了。当 webpack 编译 bundle.js 文件时会发生什么,它不会编译 C# 组件,因此它没有对我尝试访问的对象的引用。当我将应用程序作为 UWP 应用程序运行时,它运行良好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    相关资源
    最近更新 更多