C# 類庫呼叫Winform元件的方法:
阿新 • • 發佈:2021-01-28
技術標籤:C#
類庫呼叫Winform元件的方法:
首先在類庫中建立一個Logger類如下:
public static class Logger { private static int Level = 1; public static Action<string> action = default; public static void LogDebug(string msg) { Log(msg, 1); } private static void Log(string msg,int level) { if (Level <= level) { Console.WriteLine($"{DateTime.Now}:{msg}"); if (action != default) { action.BeginInvoke(msg,null,null); } } } }
然後在Winfrom端設定action
private void Form1_Load(object sender, EventArgs e)
{
Logger.action = (msg) =>
{
listBox1.Invoke(new Action(() =>
{
listBox1.Items.Add(msg);
}));
};
}
然後類庫的程式碼直接呼叫:
Logger.LogDebug()