问题描述
Here in the test method CheckAll() i am getting notifiacation in the callback class.
但是它没有点击acquisitionCallback_AcquisitionProgressEvent演示类。
but it was not hitting the acquisitionCallback_AcquisitionProgressEvent in the demo class.
非常感谢任何工作代码
public class AcquisitionCallback :IAcquisitionCallback
{
&NBSP;公共事件EventHandler< AcquisitionScanProgressInfo> AcquisitionProgressEvent;
&NBSP; public void ScanProgressUpdate(AcquisitionScanProgressInfo aInfo)
&NBSP; {
&NBSP; &NBSP; &NBSP; if(AcquisitionProgressEvent!= null)
&NBSP; &NBSP; &NBSP; {
&NBSP; &NBSP; &NBSP; &NBSP; &NBSP; AcquisitionProgressEvent.Invoke(this,aInfo);
&NBSP; &NBSP; &NBSP; }¥b $ b &NBSP; }
}
公共课程演示
{
&NBSP; public void SubscribeNotifications()
&NBSP; {
&NBSP; &NBSP; &NBSP; acquisitionCallback.AcquisitionProgressEvent + = acquisitionCallback_AcquisitionProgressEvent;
&NBSP; }
&NBSP; private void acquisitionCallback_AcquisitionProgressEvent(object sender,AcquisitionScanProgressInfo e)
&NBSP; {
$
&NBSP; }
&NBSP;私人收购回收收购回扣;
&NBSP; public bool GetDummyData()
&NBSP; {
&NBSP; &NBSP; &NBSP; SubscribeNotifications();
&NBSP; &NBSP; &NBSP; //调用一些功能 会触发通知和 等待一段时间
&NBSP; &NBSP; &NBSP;返回true;
&NBSP; }
}
公共类测试
{
&NBSP; [测试]
&NBSP; public void CheckAll()
&NBSP; {
&NBSP; &NBSP; &NBSP;演示d =新演示();
&NBSP; &NBSP; &NBSP; bool b = d.GetDummyData();
&NBSP; &NBSP; &NBSP; Assert.IsTrue(b);
&NBSP; }¥b $ b}
public class AcquisitionCallback : IAcquisitionCallback
{
public event EventHandler<AcquisitionScanProgressInfo> AcquisitionProgressEvent;
public void ScanProgressUpdate(AcquisitionScanProgressInfo aInfo)
{
if (AcquisitionProgressEvent != null)
{
AcquisitionProgressEvent.Invoke(this, aInfo);
}
}
}
public class Demo
{
public void SubscribeNotifications()
{
acquisitionCallback.AcquisitionProgressEvent += acquisitionCallback_AcquisitionProgressEvent;
}
private void acquisitionCallback_AcquisitionProgressEvent(object sender, AcquisitionScanProgressInfo e)
{
}
private AcquisitionCallback acquisitionCallback ;
public bool GetDummyData()
{
SubscribeNotifications();
//call some function which will trigger a notifications and wait for a time
return true;
}
}
public class Test
{
[Test]
public void CheckAll()
{
Demo d = new Demo();
bool b= d.GetDummyData();
Assert.IsTrue(b);
}
}
推荐答案
您的代码错误,无法调试。您可以上传可调试代码。这将有助于我们快速分析您的问题。
Your code is wrong and cannot be debugged. You can upload a debugable code. This will help us quickly analyze your problem.
我猜你正在使用一个接口来提供回调机制。所以我尝试实现以下接口回调。
I guess that you are using an Interface to provide Callback mechanism. So I tried to implement the following interface callback.
以下代码供您参考。
public class AcquisitionScanProgressInfo
{
public string value { get; set; }
}
public interface IAcquisitionCallbacktest
{
void ScanProgressUpdate(AcquisitionScanProgressInfo aInfo);
}
//Event sender
public class AcquisitionCallback : IAcquisitionCallbacktest
{
public event EventHandler<AcquisitionScanProgressInfo> AcquisitionProgressEvent;
public void ScanProgressUpdate(AcquisitionScanProgressInfo aInfo)
{
if (AcquisitionProgressEvent != null)
{
MessageBox.Show("Call Event");
AcquisitionProgressEvent.Invoke(this, aInfo);
}
}
}
//event recipient
public class Demo
{
public Demo(AcquisitionCallback Acll)
{
Acll.AcquisitionProgressEvent+= acquisitionCallback_AcquisitionProgressEvent;
}
private void acquisitionCallback_AcquisitionProgressEvent(object sender, AcquisitionScanProgressInfo e)
{
MessageBox.Show("Call back event : "+e.value);
}
}
// Now to trigger the event
AcquisitionCallback Acall = new AcquisitionCallback();
Demo demost = new Demo(Acall);
AcquisitionScanProgressInfo a = new AcquisitionScanProgressInfo();
a.value = "0099008";
Acall.ScanProgressUpdate(a);
最好的问候,
Yohann Lu
Yohann Lu
这篇关于回调不在类的对象中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!