通过 Corona 运行时事件在 Lua 环境和 .NETCoronaPanel
XAML 控件加载 Corona 项目时,它会引发一个“已加载”事件,提供 CoronaRuntimeEnvironment
类的新实例。此实例表示项目的已加载 Lua 环境,使用此对象,可以在 Lua 中侦听并派发 Corona 运行时事件。
以下示例说明了 C# 请求 Lua 将两个数字相加并将结果返回到 C#。通过让 C# 向 Lua 派发一个名为 “requestingSum”
的事件,该事件带有两个数字属性,就可以实现这一点。Lua 侦听器会接收到此事件,然后将和返回到 C#。
// [C#] /// <summary>Constructor. Initializes member variables and the user interface.</summary> public MainPage() { // Initialize this page's components that were set up via the UI designer. InitializeComponents(); // Set up Corona to automatically start up when the control's Loaded event has been raised. fCoronaPanel.AutoLaunchEnabled = true; // Add a Corona event handler which detects when the Corona project has been started. fCoronaPanel.Runtime.Started += OnCoronaRuntimeStarted; } /// <summary>Called when the Corona runtime has started and finished loaded the "main.lua" file.</summary> /// <param name="sender">The CoronaRuntime object that raised this event.</param> /// <param name="e">Event arguments providing the CoronaRuntimeEnvironment that has been started.</param> private void OnCoronaRuntimeStarted(object sender, CoronaLabs.Corona.WinRT.CoronaRuntimeEventArgs e) { // Create a custom Corona event named "requestingSum" with the following properties. // This will be converted into a Lua "event" table once dispatched by Corona. var eventProperties = CoronaLabs.Corona.WinRT.CoronaLuaEventProperties.CreateWithName("requestingSum"); eventProperties.Set("x", 5); eventProperties.Set("y", 10); eventProperties.Set("message", "Hello. Can you add these 2 numbers please?"); // Dispatch the event to Lua. var eventArgs = new CoronaLabs.Corona.WinRT.CoronaLuaEventArgs(eventProperties); var result = e.CoronaRuntimeEnvironment.DispatchEvent(eventArgs); // Check if a number was returned from the Lua listener. var boxedNumber = result.ReturnedValue as CoronaLabs.Corona.WinRT.CoronaBoxedNumber; if (boxedNumber != null) { // A number was returned from Lua. Fetch it. double sum = boxedNumber.Value; } }
-- [Lua] -- Called when a Corona event named "requestingSum" has been dispatched local function onRequestingSum( event ) -- Print event property "message" to Visual Studio's Output Panel print( event.message ) -- Sum event properties "x" and "y" and return the result back to C# return event.x + event.y end -- Set up the above function to be called when an event named "requestingSum" has been dispatched Runtime:addEventListener( "requestingSum", onRequestingSum )
以下示例说明了 Lua 请求 C# 通过派发 “requestingMessageBox”
事件显示本机消息框。当 Corona 运行时加载时,应用程序的 C# 端会设置 Corona 事件侦听器 - 这是添加 Corona 运行时事件侦听器的最佳时机,因为“已加载”事件在执行 main.lua
之前在 C# 中引发。
// [C#] /// <summary>Constructor. Initializes member variables and the user interface.</summary> public MainPage() { // Initialize this page's components that were set up via the UI designer. InitializeComponents(); // Set up Corona to automatically start up when the control's Loaded event has been raised. fCoronaPanel.AutoLaunchEnabled = true; // Add a Corona event handler which detects when the Corona project has been loaded, but not started yet. fCoronaPanel.Runtime.Loaded += OnCoronaRuntimeLoaded; } /// <summary> /// Called when a new CoronaRuntimeEnvironment has been created/loaded, /// but before the "main.lua" has been executed. /// </summary> /// <param name="sender">The CoronaRuntime object that raised this event.</param> /// <param name="e">Event arguments providing the CoronaRuntimeEnvironment that has been created/loaded.</param> private void OnCoronaRuntimeLoaded(object sender, CoronaLabs.Corona.WinRT.CoronaRuntimeEventArgs e) { // Set up C# method OnRequestingMessageBox() to be invoked when Corona runtime event // "requestingMessageBox" has been dispatched. e.CoronaRuntimeEnvironment.AddEventListener("requestingMessageBox", OnRequestingMessageBox); } /// <summary>Called when Corona runtime event "requestingMessageBox" has been dispatched.</summary> /// <param name="sender">The CoronaRuntimeEnvironment that dispatched the event.</param> /// <param name="e">Provides the Lua event table's fields/properties.</param> private CoronaLabs.Corona.WinRT.ICoronaBoxedData OnRequestingMessageBox( CoronaLabs.Corona.WinRT.CoronaRuntimeEnvironment sender, CoronaLabs.Corona.WinRT.CoronaLuaEventArgs e) { // Fetch the "event.message" property. var boxedMessage = e.Properties.Get("message") as CoronaLabs.Corona.WinRT.CoronaBoxedString; if (boxedMessage == null) { // A "message" property was not provided or it was not of type string. // Return an error message to Lua describing what went wrong. return CoronaLabs.Corona.WinRT.CoronaBoxedString.From("'event.message' is a required field."); } // Display a native message box with the given string. System.Windows.MessageBox.Show(boxedMessage.ToString()); // Return a success message to Lua. return CoronaLabs.Corona.WinRT.CoronaBoxedString.From("Message box was displayed successfully!"); }
-- [Lua] -- Dispatch an event named "requestingMessageBox" to be received by C# local requestingMessageBoxEvent = { name = "requestingMessageBox", message = "Hello World!" } local result = Runtime:dispatchEvent( requestingMessageBoxEvent ) -- Print the message returned by C# print( tostring(result) )
此处可以找到 Corona .NET/C++/CX 类框架的文档 在此处。
⟨ 指南索引