Here are some more details if it helps you understand how this works. Calling CoronaRuntimeEnvironment.DispatchEvent(), CoronaRuntimeEnvironment.AddEventLister(), and CoronaRuntimeEnvironment.RemoveEventListener() will literally call Lua functions Runtime:dispatchEvent(), Runtime:addEventListener(), and Runtime:removeEventListener() under the hood. We’re merely bridging to these functions between .NET and Lua.
For example, the following dispatches and listens to a custom event purely on the Lua side. This is what’s typically done with Lua libraries such as widgets, composer, etc.
-- Set up a custom event listener. local function onCustomEventReceived(event) print("Received custom event") end Runtime:addEventListener("customEvent", onCustomEventReceived) -- Dispatch the custom event. Runtime:dispatchEvent({name="customEvent"})
And here is the equivalent of the above completely written in C#. Note that this adds an event listener and dispatches an event completely on the C# side (nothing written on the Lua side). Not a practical example, but it demonstrates the concept of how our event dispatcher/listener system works.
void OnCoronaRuntimeLoaded(object sender, CoronaRuntimeEventArgs e) { // Add a custom event listener. e.CoronaRuntimeEnvironment.AddEventListener("customEvent", OnCustomEventReceived); // Dispatch the custom event. var eventProperties = CoronaLuaEventProperties.CreateWithName("customEvent"); var eventArgs = new CoronaLuaEventArgs(eventProperties); e.CoronaRuntimeEnvironment.DispatchEvent(eventArgs); } ICoronaBoxedData OnCustomEventReceived(CoronaRuntimeEnvironment sender, CoronaLuaEventArgs e) { System.Diagnostics.Debug.WriteLine("Received custom event"); }
Does this help any?