1. 事件 Event
- 定义一个事件类
- 调用触发事件接口
- 注册触发回调函数
public class MyEventManager
{
private static MyEventManager instance = null;
private MyEventManager() { }
public static MyEventManager Instance
{
get
{
if (instance == null)
{
instance = new MyEventManager();
}
return instance;
}
}
public delegate void TestHander(string time, string message);
public event TestHander TestEvent;
public void TestEvent(string time, string message)
{
if (this.TestEvent!= null)
{
orderDownEvent(time, message);
}
}
}
MyEventManager.Instance.orderDownDoaction(DateTime.Now.ToString("yyyyMMddHHmmssm"), message);
MyEventManager.Instance.TestEvent += onTestEvent ;
private void containerPickStartTest(string time, string message)
{
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal,
new Action(() =>
{
}));
}
2. 委托
- 定义一个委托类型
- 调用触发委托接口
- 注册触发回调函数
public delegate void ITestDelegate(string strJson, ref BackStruct result);
public class ServerService
{
private static ServerService instance = null;
private ServerService() { }
public static ServerService Instance
{
get
{
if (instance == null)
{
instance = new ServerService();
}
return instance;
}
}
public ITestDelegate Test1Delegte;
public ITestDelegate Test2Delegte;
internal void Test1Reglster(ITestDelegate del)
{
Test1Delegte = del;
}
internal void Test2Reglster(ITestDelegate del)
{
Test2Delegte = del;
}
}
BackStruct = new BackStruct();
ServerService.Instance.orderDownDelegte(strJson, ref back);
ServerService.Instance.Test1Reglster(new ITestDelegate (onTest1Callbcak));
ServerService.Instance.Test2Reglster(new ITestDelegate (onTest2Callbcak));
private void onTest1Callbcak(string strJson, ref BackStruct result)
{
}
private void onTest2Callbcak(string strJson, ref BackStruct result)
{
}