1. 把UI类中的方法通过委托传给f1类,实现f1类调用UI类中的方法(实现例如更新UI、数据处理的功能)
2. 此方法缺点,类与类之间存在过度耦合,后续需开发更好方法
namespace Prog
{
public delegate void myDataDelegate(byte[] buf, int length);
public class f1
{
private myDataDelegate myDataFunc;
byte[] buf;
int length;
public void f1(myDataDelegate delegate1)
{
myDataFunc = delegate1;
}
public void func()
{
buf = new byte[100];
...
length = 10;
myDataFunc(buf, length);
}
}
}
namespace Prog
{
public partial class Form1 : Form
{
myDataDelegate myDataFuncF;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
myDataFuncF = new myDataDelegate(DataParseInvoke);
f1 f1Instance = new f1(myDataFuncF);
f1Instance.func();
}
private void DataParseInvoke(byte[] buf, int length)
{
Invoke((EventHandler)(delegate
{
DataParse(buf, length);
})
);
}
private void DataParse(byte[] buf, int length)
{
}
}
}