开发者

Event handler doesn't execute after subscribing new event handler

I have a MVVM application in WPF. I have an event that gets dispatc开发者_StackOverflow社区hed from a static instance of a class.

When a given condition occurs, I need to listen for the next occurrence of said event. I do this with the following code:

myInstance.OnData += myEventHandlerInstance;

The event handler is removed at a later time, but as soon as it is added, the event handlers in the other view models no longer execute. I have verified that the dispatcher is the same instance in all places where the event is fired and handled (using the methods described below).

Why does the existing handler not get executed?


delete this line:

myInstance.OnData -= new EventHandler(myInstance_onData);

Why were you doing this? You should get the original event handler and remove that. Also, you should do this outside the event handler itself.


You'll have to put in something like this, to ensure that your code is executing on the right thread:

if(Dispatcher.CurrentDispatcher.CheckAccess())
{
   <Code here>
}
else
{
   Dispatcher.CurrentDispatcher.Invoke(<Code here>)
}


Thanks for the help everyone. You helped me focus in on the problem. The problem was purely a structure/logic issue, I was able to dig deeper into the code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜