Note that there are some explanatory texts on larger screens.

plurals
  1. POEvent handling with an anonymous delegate
    text
    copied!<p>For the record: I found a similar question <a href="https://stackoverflow.com/questions/1348150/how-do-i-unregister-anonymous-event-handler" title="How do I unregister anonymous event handler">here</a> but I have to elaborate a bit more on on the subject.</p> <p>My concrete scenario is this:</p> <p>In Silverlight 4, The <code>myFrameworkElement.FindName("otherElementName")</code> method seems to work fine now, but I encountered a problem. It still returns <code>null</code> when the element is not yet added to the visual tree obviously.</p> <p>But now I need this functionality in a <code>DependencyProperty</code>'s <code>PropertyChangedCallback</code> of a custom <code>UserControl</code> handler. In this scope, it is uncertain if the UserControl is added to the visual tree yet. But I must execute a certain action on another element in the tree. When the element is already available, it can and should be done right now. If not, it must be done immediately when it is available. So I came up with this extension method that I can call like this:</p> <pre><code>myFrameworkElement.FindNameEnsured("otherElementName", result =&gt; this.DoSomethingWith(result)); </code></pre> <p>The code for the extension method goes like this:</p> <pre><code> static public void FindNameEnsured(this FrameworkElement self, string name, Action&lt;object&gt; resultAction) { if (self != null &amp;&amp; resultAction != null) { object result = self.FindName(name); if (result != null) { resultAction(result); } else { RoutedEventHandler handler = null; handler = (sender, e) =&gt; { result = self.FindName(name); resultAction(result); self.Loaded -= handler; }; self.Loaded += handler; } } </code></pre> <p>As you can see, I must use an anonymous delegate because I need the values for <code>name</code> and <code>resultAction</code> inside of the handler. I then go unsubscribe the event inside of the handler because I'm a smart and clean guy and want no leakage. I also don't want to break any flies on wheels here with some fancy WeakEventFactories or similar stuff.</p> <p>Now this works smoothly so far. But I have some questions.</p> <ol> <li>Is this generally a clean enough approach to unsubscribe the event handler inside of the handler? Or is that going to kill an innocent puppy eventually?</li> <li>Could there be some issues like some leakage because of using outer scope variables inside the anonymous delegate?</li> <li>Can there be thread synchronization issues which would cause me to "miss" the <code>Loaded</code> event? In this special scenario, only the Silverlight's UI dispatcher thread should be involved. But if it's a problem anyway, and/or if I need a similar functionality in a non-UI related scenario, what's the best approach to f1x0r it?</li> </ol> <p>Thanks already for your patience and time reading my lengthy elaborations. ;-)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload