在xLua脚本内加载三方dll:xlua.load_assembly('aLuaTestDll') --不用加.dll后缀
使用C#内部类:CS.xxx.xxx.xxx CS前缀是固定的,后面命名空间和类名按c#调用约定拼写
public xLuaForm()
{
InitializeComponent();
_luaEnv = new LuaEnv();
//_luaEnv.
}
public void Print(string msg)
{
MessageBox.Show(msg);
}
private LuaEnv _luaEnv;
public static void StaticMethod(string msg)
{
MessageBox.Show(msg);
}
private void button1_Click(object sender, EventArgs e)
{
var script = $@"
xlua.load_assembly('NLuaTestDll')
CS.System.Windows.Forms.MessageBox.Show('hello001')
local newForm = CS.System.Windows.Forms.Form()
newForm:ShowDialog();
newForm:Dispose();
person = CS.NLuaTestDll.Person()
person.Name = 'Winter'
person.Age = 10
CS.WindowsFormsApp1.xLuaForm.StaticMethod(person:ToString())
participant = {{""张三"", ""李四"", ""老王"", ""狗蛋"", ""铁剩""}}
CS.WindowsFormsApp1.xLuaForm.StaticMethod('abc')
--require 'CSharpCallLua'
--person = {{Name = 'Tom', Age = 10}}
name = 'jerry'
form.Text = 'lua test'
form:Print('hello ' .. #participant)
function add(a, b)
return a+b
end
";
_luaEnv.Global["form"] = this;
_luaEnv.DoString(script);
var p = _luaEnv.Global.Get<Person>("person");
MessageBox.Show(p.ToString());
}
namespace NLuaTestDll
{
[LuaCallCSharp]
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string ToString()
{
return $"{Name} -- {Age}";
}
}
}
local doc = CS.System.Xml.XmlDocument();
doc:Load("d:\\123.xml");
local node = doc:SelectSingleNode("RootConfig/ConfigList");
local subNodeCnt = node.ChildNodes.Count
CS.System.Windows.Forms.MessageBox.Show(tostring(subNodeCnt))
for i = 1, subNodeCnt do
local subNode = node.ChildNodes[i]
CS.System.Windows.Forms.MessageBox.Show(subNode.Attributes:GetNamedItem('ConfiglName').InnerText);
local alarmSourceNode = subNode:SelectSingleNode('SubNode1')
CS.System.Windows.Forms.MessageBox.Show(alarmSourceNode.InnerText)
end
xLua使用c#的XmlDocument类解析xml文件,使用node.Attributes["xxx"]失败,改为调用node.Attributes:GetNamedItem可以正常读取节点属性
public void SetButtonClickMethod(Button button, string luaMethodName)
{
var method = _luaEnv.Global.Get<EventHandler>(luaMethodName);
button.Click += method;
}
public void DelButtonClickMethod(Button button, string luaMethodName)
{
var method = _luaEnv.Global.Get<EventHandler>(luaMethodName);
button.Click -= method;
}
xLua内创建winform控件,进行事件订阅遇到麻烦,需要c#内写函数协助,如上定义一个SetButtonClickMethod,由xLua调用,传递Button对象实例,xLua函数名称,即可实现将xLua函数订阅Button Click事件。还不够灵活,暂可达到预期目标。
function button_click(sender, e)
CS.System.Windows.Forms.MessageBox.Show('hello001')
Close();
end
local newForm = CS.System.Windows.Forms.Form()
local button = CS.System.Windows.Forms.Button()
button.Width = 100;
button.Height = 50;
button.Text = "OK";
button.Parent = newForm;
button.Location = CS.System.Drawing.Point(10, 10)
--button.DialogResult= CS.System.Windows.Forms.DialogResult.OK;
form:SetButtonClickMethod(button,"button_click")
newForm:ShowDialog();
form:DelButtonClickMethod(button,"button_click")
newForm:Dispose();
调用当前项目托管库功能
xlua.load_assembly('CommonLib') --导入CommonLib.dll
CS.xxxx.CommonLib.IoHelper.CopyDirectory("D:\\Dir1","D:\\Dir1Bak",true) --调用CommonLib dll中的函数做目录深拷贝