我在 google 上找到了如何在 Javascript 代码中实现 C# 事件的事件监听器的示例,但不知何故,在我的例子中,似乎没有任何效果。
我的 C# 类(class):
namespace Engine
{
public delegate void EventDelegate();
public sealed class MyClass;
{
public event EventDelegate nowEvent;
public void runJobs()
{
if (nowEvent == null)
{
Debug.WriteLine("What? Nobody subscribed???");
}
}
}
}
Javascript 方面:
WinJS.Namespace.define("App", {
myclass: new Engine.MyClass()
});
App.myclass.addEventListener("nowEvent", function onReady() {
console.error("hello");
});
App.myclass.runJobs();
将 onReady 函数附加到 nowEvent 事件并运行 runJobs() 后,MyClass.nowEvent 为 null。
在 JS 中订阅自己类的 C# 事件的工作语法是什么?有什么想法吗?
编辑:根据 Gambit 的建议:
App.myclass.addEventListener("nowevent", function onReady() {
console.error("hello");
});
事件指针不再为空,但是当我触发事件时,我得到:
Engine.winmd 中发生了“System.InvalidCastException”类型的第一次机会异常
最佳答案
您可能会遇到的是投影更改所使用语言的名称大小写的方式。对于 Javascript,事件的约定是小写。因此,在 C# 中,您将事件定义为“nowEvent”,在 Javascript 中,您将其称为“nowevent”。
App.myclass.addEventListener("nowevent", function onReady() {
console.error("hello");
});
关于c# - 如何在 Win8 Metro 应用程序中的 Javascript 代码中捕获自己的 C# 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12321864/