TL;博士
实施班级:
public Main()
{
Foo foo = new Foo();
foo.OnBarOneResponse += foo_OnBarOneResponse;
foo.OnBarTwoResponse += foo_OnBarTwoResponse;
foo.FetchBarOne();
}
void foo_OnBarOneResponse(String response)
{
// Called successfully.
this.foo.FetchBarTwo();
}
void foo_OnBarTwoResponse(String response)
{
// Never called :(
}
Foo.cs
private MyJavascriptInjector _javascriptInjector = new MyJavascriptInjector();
public delegate void OnBarOneResponseHandler(String response);
public delegate void OnBarTwoResponseHandler(String response);
public event OnBarOneResponseHandler OnBarOneResponse = delegate { };
public event OnBarTwoResponseHandler OnBarTwoResponse = delegate { };
private void _onBarOneResponse(String response)
{
// Called Successfully
OnBarOneResponse(response);
}
private void _onBarTwoResponse(String response)
{
// Never called :(
OnBarTwoResponse(response);
}
public Foo()
{
webBrowser.ObjectForScripting = _javascriptInjector;
_javascriptInjector.OnBarOneResponse += _onBarOneResponse;
_javascriptInjector.OnBarTwoResponse += _onBarTwoResponse;
webBrowser.Navigate("http://myurl", null, new Byte[0], myHeaders");
}
public void FetchBarOne()
{
webBrowser.InvokeScript("fetchBarOne");
}
public void FetchBarTwo()
{
webBrowser.InvokeScript("fetchBarTwo");
}
MyJavascriptInjector.cs
[System.Runtime.InteropServices.ComVisible(true)]
public class MyJavascriptInjector
{
public delegate void OnBarOneResponseHandler(string response);
public delegate void OnBarTwoResponseHandler(string response);
public event OnBarOneResponseHandler OnBarOneResponse;
public event OnBarTwoResponseHandler OnBarTwoResponse;
public void OnBarOneResponse(String response)
{
// Called successfully!
OnBarOneResponse(response);
}
public void OnBarTwoResponse(String response)
{
// ALSO CALLED SUCCESSFULLY BUT WHEN CALLING THIS Foo.cs event NEVER GETS FIRED.
// IT GETS LOST SOMEWHERE BETWEEN HERE and Foo.cs!
OnBarTwoResponse(response);
}
}
=================
我有一个对象 Foo ,它有两种方法, FetchBarOne 和 FetchBarTwo 。
每种方法都有一个事件, OnBarOneResponse 和 OnBarTwoResponse 。
实现类使用符号" + ="在其构造函数中注册 Foo 的事件。并为每个定义一个回调函数: foo_OnBarOneResponse(字符串响应)和 foo_OnBarTwoResponse(字符串响应)。
问题:
实施类遵循以下内容:
this.foo.FetchBarOne();
foo_OnBarOneResponse(String response)
将在以后解雇。this.foo.FetchBarTwo();
foo_OnBarTwoResponse(String response)
永远不会开火。更多信息:
Foo 已经包裹WebBrowser并正在调用InvokeScript以在已加载的网页中执行javascript。此网页上有许多javascript函数,包括 FetchBarOne 和 FetchBarTwo 。调试时,会调用 FetchBarTwo 并成功响应数据。但是,在 WebBrowser 成功返回数据后,当 Foo 调用实现类注册的内部 OnBarTwoResponseHandler 事件委托时,它会获得&# 34;丢失"在介于两者之间的某个地方 - 即使它不是空的,它显然有一个参考。
未解决的未解决问题
Dispatcher.Invoke(DispatcherPriority.Normal, new ThreadStart(() => this.foo.FetchBarTwo()));
尝试从UI线程调用它。没有成功。如果有人对此事有任何想法,我将非常感激。谢谢!
答案 0 :(得分:0)
this.foo
字段 不与foo
变量相同的实例。
从你的例子:
public Main()
{
Foo foo = new Foo();
foo.OnBarOneResponse += foo_OnBarOneResponse;
foo.OnBarTwoResponse += foo_OnBarTwoResponse;
foo.FetchBarOne();
}
void foo_OnBarOneResponse(String response)
{
// Called successfully.
this.foo.FetchBarTwo();
}
尝试使用您的字段(未在您的示例中包含):
private Foo foo;
public Main()
{
this.foo = new Foo();
this.foo.OnBarOneResponse += foo_OnBarOneResponse;
this.foo.OnBarTwoResponse += foo_OnBarTwoResponse;
this.foo.FetchBarOne();
}