2 回答
TA贡献1856条经验 获得超5个赞
使用事件:
应用程序A:
public void MyAppMethod(string inComing){
Debug.WriteLine("callback returned: " + inComing);
}
B.MyAppMethod += MyAppMethod;
应用程序B:
public event Action<string> MyAppMethod;
private void SomeMethod(){
//do some stuff
if(MyAppMethod!=null) MyAppMethod("hello");
}
使用委托:
应用程序A:
public void MyAppMethod(string inComing){
Debug.WriteLine("callback returned: " + inComing);
}
B.MyAppMethod = MyAppMethod;
应用程序B:
public Action<string> MyAppMethod;
private void SomeMethod(){
//do some stuff
if(MyAppMethod!=null) MyAppMethod("hello");
}
这些代码将按原样工作。但是,在使用此代码之前,您必须考虑许多重要事项,您可以在网上找到这些内容
TA贡献1966条经验 获得超4个赞
这是我最终使用的代码。
应用程序一:
public void MyCallBack(string status)
{
// Add callback text to the status window
StatTxt.AppendText(status);
}
// Set the callback method in DLL B
B.HollaBack = MyCallBack;
动态链接库 B:
public delegate void CallBackDelegate(string status);
public CallBackDelegate HollaBack
{
get; set;
}
private void DoCallBack(string inMsg)
{
// Make sure the callback method was set
if (HollaBack != null)
{
// send text to the callback method
HollaBack(inMsg);
}
}
// Invoke the callback anywhere within DLL B
DoCallBack("show this in the status window");
- 2 回答
- 0 关注
- 239 浏览
添加回答
举报