ActionMessage.ElementLoaded

Jul 26, 2010 at 5:59 PM
Hi, There are some situations in which the ElementLoaded event for a control occurs twice. It might have something to do with how AvalonDock handles content. The first time the event occurs, the VisualTree seems to be incomplete, and ActionMessage.GetMethodBinding is unable to climb all the way to the element associated with the ViewModel. But on the second occurrence it is able to find the correct element. I have been swallowing the "no target found for method..." exception thrown by the ElementLoaded event handler, but in some situations it causes window refresh problems. However, if I just comment out the "throw", everything is happy. Would it be possible to eliminate this exception and just log a warning? Or perhaps make it interceptable through the boostrapper? By the way, I just noticed that the problem only occurs when I don't rely on conventions... <Button Name="Cmd_Delete" cal:Message.Attach="[Event Click] = [Action Cmd_Delete()]" /> The following does not cause the exception... <Button Name="Cmd_Delete" />
Jul 26, 2010 at 6:11 PM
I lied, all is not happy when the button is declared inside an ItemTemplate. It needs an explicit Message.Attach in that case. So I do need a way to keep that Exception from being thrown.
Coordinator
Jul 26, 2010 at 6:12 PM

If you update to the latest revision, you will see that I've made some major improvements to the extensibility of ActionMessage. In your case, I would replace ActionMessage.PrepareContext with your own implementation, mirroring the out-of-the-box implementation, minus the exception. I've found the exception to help prevent developer errors and I'm inclined to you think you might have a special scenario. So, I want to leave it for the time being. If I see other users having this same issue, I'll replace it with a warning. Is that a reasonable compromise?

Jul 26, 2010 at 6:40 PM
Edited Jul 26, 2010 at 6:41 PM
Oh! I now see in the latest version that you moved this to a delegate PrepareContext that I can replace with my own "exception free" version. Fantastic! edit: wrote that before seeign your reply =)