I found examples with google how to implement event listeners to C# events in Javascript code, but somehow in my case, nothing seems to work.
My 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 side:
WinJS.Namespace.define("App", {
myclass: new Engine.MyClass()
});
App.myclass.addEventListener("nowEvent", function onReady() {
console.error("hello");
});
App.myclass.runJobs();
After attaching the onReady function to the nowEvent event and running runJobs(), the MyClass.nowEvent is null.
What is the working syntax to subscribe to C# events of own classes in JS. Any ideas?
EDIT: After Gambit’s advise:
App.myclass.addEventListener("nowevent", function onReady() {
console.error("hello");
});
The event pointer is not null anymore, but when I fire the event, I get:
A first chance exception of type ‘System.InvalidCastException’ occurred in Engine.winmd
What you might be running into is the way the projection changes the casing of names for the language being used. For Javascript, the convention for events is lowercase. So in C#, you defined the event as “nowEvent”, in Javascript you refer to it as “nowevent”.