<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.silverlight.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Dynamically Creating User Controls That Fire Events Back To You</title><link>http://blogs.silverlight.net/blogs/jesseliberty/archive/2008/05/07/dynamically-creating-user-controls-that-fire-events-back-to-you.aspx</link><description>Aha! Okay, I may not fully understand all the requirements, but the following demo will show how to dynamically create a user control and then have that user control close itself, remove itself from the containing page and fire an event to the page so</description><dc:language>en</dc:language><generator>CommunityServer 2007 (Build: 20416.853)</generator><item><title>re: Dynamically Creating User Controls That Fire Events Back To You</title><link>http://blogs.silverlight.net/blogs/jesseliberty/archive/2008/05/07/dynamically-creating-user-controls-that-fire-events-back-to-you.aspx#219818</link><pubDate>Thu, 14 May 2009 13:37:24 GMT</pubDate><guid isPermaLink="false">d0d632c8-a6f7-4f68-b0ce-26aaafd62132:219818</guid><dc:creator>donmarais</dc:creator><description>&lt;p&gt;Jesse,&lt;/p&gt;
&lt;p&gt;I have a complex situation with nested children objects being loaded into UI to create a an image display. The parent is MyControl. It loads a child, MyTemplate, which in turn loads in MyBox, which in turn opens a popup, depending on user selection of image. Popup has close button and button to select image. Select image event handler to close all of MyControl's children and load in MyEditor in its place. Incidently, MyControl has a grid Main, into which all these children are being loaded in. However, all that happens is that popup is closed. Someone suggested using delegate events to call a method on MyControl to close its children and load in MyEditor. Can you provide an example of how to do this please?&lt;/p&gt;
&lt;img src="http://blogs.silverlight.net/aggbug.aspx?PostID=219818" width="1" height="1"&gt;</description></item><item><title>re: Dynamically Creating User Controls That Fire Events Back To You</title><link>http://blogs.silverlight.net/blogs/jesseliberty/archive/2008/05/07/dynamically-creating-user-controls-that-fire-events-back-to-you.aspx#52429</link><pubDate>Sat, 10 May 2008 20:41:50 GMT</pubDate><guid isPermaLink="false">d0d632c8-a6f7-4f68-b0ce-26aaafd62132:52429</guid><dc:creator>jesseliberty</dc:creator><description>&lt;p&gt;Ben,&lt;/p&gt;
&lt;p&gt;I double checked, you do not have to take extra steps either to ensure that the parent page will receive the notification, nor to ensure that the control will be destroyed; simply fire the event and remove the control from the parent's collection and it will work as it should. &lt;/p&gt;
&lt;img src="http://blogs.silverlight.net/aggbug.aspx?PostID=52429" width="1" height="1"&gt;</description></item><item><title>re: Dynamically Creating User Controls That Fire Events Back To You</title><link>http://blogs.silverlight.net/blogs/jesseliberty/archive/2008/05/07/dynamically-creating-user-controls-that-fire-events-back-to-you.aspx#52077</link><pubDate>Thu, 08 May 2008 21:20:10 GMT</pubDate><guid isPermaLink="false">d0d632c8-a6f7-4f68-b0ce-26aaafd62132:52077</guid><dc:creator>BenHayat</dc:creator><description>&lt;p&gt;&amp;gt;&amp;gt;or is the fact that the page is registered with the event handler enough to keep it alive. For this, I need to make inquiries.&amp;lt;&amp;lt;&lt;/p&gt;
&lt;p&gt;This is something that's been in my head. So I figured, just to be on the safe side, I'd do two extra step to ensure the object and it's original reference has been destroyed, one by deregetering the event and secondly setting the reference (auc) to null. Because if the user clicks on the create button again, a new object is created and is hooked up to reference and then gets added to children. &lt;/p&gt;
&lt;p&gt;When I used to write in Delphi for Win32 (which didn't have it's own GC), we had to be very careful about leaving objects around.&lt;/p&gt;
&lt;p&gt;Thanks to you, this blog has been very valuable [to me at least].&lt;/p&gt;
&lt;img src="http://blogs.silverlight.net/aggbug.aspx?PostID=52077" width="1" height="1"&gt;</description></item><item><title>re: Dynamically Creating User Controls That Fire Events Back To You</title><link>http://blogs.silverlight.net/blogs/jesseliberty/archive/2008/05/07/dynamically-creating-user-controls-that-fire-events-back-to-you.aspx#52010</link><pubDate>Thu, 08 May 2008 15:15:42 GMT</pubDate><guid isPermaLink="false">d0d632c8-a6f7-4f68-b0ce-26aaafd62132:52010</guid><dc:creator>jesseliberty</dc:creator><description>&lt;p&gt;&amp;gt;&amp;gt;Even though I may still have my original Reference auc hanging around, but that doesn't mean it's pointing to the old object any more. And if it is not, then GC will eventually remove the auc too.&amp;lt;&amp;lt;&lt;/p&gt;
&lt;p&gt;You certainly did. Now, the question is whether I have the order of operations correct and what happens if there are no other references to that object... that is, if the GC cleans up the object is there a chance it won't fire its event, or is the fact that the page is registered with the event handler enough to keep it alive. For this, I need to make inquiries.&lt;/p&gt;
&lt;img src="http://blogs.silverlight.net/aggbug.aspx?PostID=52010" width="1" height="1"&gt;</description></item><item><title>Dew Drop - May 8, 2008 | Alvin Ashcraft's Morning Dew</title><link>http://blogs.silverlight.net/blogs/jesseliberty/archive/2008/05/07/dynamically-creating-user-controls-that-fire-events-back-to-you.aspx#51974</link><pubDate>Thu, 08 May 2008 13:19:09 GMT</pubDate><guid isPermaLink="false">d0d632c8-a6f7-4f68-b0ce-26aaafd62132:51974</guid><dc:creator>Dew Drop - May 8, 2008 | Alvin Ashcraft's Morning Dew</dc:creator><description>&lt;p&gt;Pingback from &amp;nbsp;Dew Drop - May 8, 2008 | Alvin Ashcraft's Morning Dew&lt;/p&gt;
&lt;img src="http://blogs.silverlight.net/aggbug.aspx?PostID=51974" width="1" height="1"&gt;</description></item><item><title>re: Dynamically Creating User Controls That Fire Events Back To You</title><link>http://blogs.silverlight.net/blogs/jesseliberty/archive/2008/05/07/dynamically-creating-user-controls-that-fire-events-back-to-you.aspx#51880</link><pubDate>Thu, 08 May 2008 03:33:44 GMT</pubDate><guid isPermaLink="false">d0d632c8-a6f7-4f68-b0ce-26aaafd62132:51880</guid><dc:creator>BenHayat</dc:creator><description>&lt;p&gt;Oh wait, let me take back my previous question.&lt;/p&gt;
&lt;p&gt;When we add a User Control to a collection (even though as part of the constructor, we pass the reference to that object to the collection (auc)), but from that point on, the collection now maintains (as you said) a new internal reference to the User Control object and then when we remove the object from collection, the internal collection reference and the User Control object are destroyed. Even though I may still have my original Reference auc hanging around, but that doesn't mean it's pointing to the old object any more. And if it is not, then GC will eventually remove the auc too.&lt;/p&gt;
&lt;p&gt;Did I get right? &lt;/p&gt;
&lt;img src="http://blogs.silverlight.net/aggbug.aspx?PostID=51880" width="1" height="1"&gt;</description></item><item><title>re: Dynamically Creating User Controls That Fire Events Back To You</title><link>http://blogs.silverlight.net/blogs/jesseliberty/archive/2008/05/07/dynamically-creating-user-controls-that-fire-events-back-to-you.aspx#51875</link><pubDate>Thu, 08 May 2008 03:22:53 GMT</pubDate><guid isPermaLink="false">d0d632c8-a6f7-4f68-b0ce-26aaafd62132:51875</guid><dc:creator>BenHayat</dc:creator><description>&lt;p&gt;&amp;gt;&amp;gt;No, there is no necessity for that. In fact, you'll notice that I reuse that reference to point to a second instance. &amp;nbsp;The reference is maintained by the colleciton and when I remove it from the collection, there is no longer a refernece to it.&amp;lt;&amp;lt;&lt;/p&gt;
&lt;p&gt;So, you're saying when we create a User Control and add the reference to Grid's collection the User Control gets created and then when we remove the reference from from the collection, the actual User Control gets destroyed.&lt;/p&gt;
&lt;p&gt;And if I add that reference back to the collection, the user control object gets created again?&lt;/p&gt;
&lt;img src="http://blogs.silverlight.net/aggbug.aspx?PostID=51875" width="1" height="1"&gt;</description></item><item><title>re: Dynamically Creating User Controls That Fire Events Back To You</title><link>http://blogs.silverlight.net/blogs/jesseliberty/archive/2008/05/07/dynamically-creating-user-controls-that-fire-events-back-to-you.aspx#51870</link><pubDate>Thu, 08 May 2008 03:00:02 GMT</pubDate><guid isPermaLink="false">d0d632c8-a6f7-4f68-b0ce-26aaafd62132:51870</guid><dc:creator>jesseliberty</dc:creator><description>&lt;p&gt;&amp;gt;&amp;gt; The only way for GC to see that, is if auc=null &amp;lt;&amp;lt;&lt;/p&gt;
&lt;p&gt;No, there is no necessity for that. In fact, you'll notice that I reuse that reference to point to a second instance. &amp;nbsp;The reference is maintained by the colleciton and when I remove it from the collection, there is no longer a refernece to it.&lt;/p&gt;
&lt;p&gt;I don't know if having the registration on the event handler matters; I suppose it must because the page has to be able to get to the event even though the control has been removed from its children collection. I'd have to look into the order of operations, but it would be a simple matter to deregister it while cleaning up.&lt;/p&gt;
&lt;p&gt;In any case, no problem; it was a fun project and a good blog topic. I'll come back to it with more time and make it into a video at some point.&lt;/p&gt;
&lt;img src="http://blogs.silverlight.net/aggbug.aspx?PostID=51870" width="1" height="1"&gt;</description></item><item><title>re: Dynamically Creating User Controls That Fire Events Back To You</title><link>http://blogs.silverlight.net/blogs/jesseliberty/archive/2008/05/07/dynamically-creating-user-controls-that-fire-events-back-to-you.aspx#51866</link><pubDate>Thu, 08 May 2008 02:40:12 GMT</pubDate><guid isPermaLink="false">d0d632c8-a6f7-4f68-b0ce-26aaafd62132:51866</guid><dc:creator>BenHayat</dc:creator><description>&lt;p&gt;&amp;gt;&amp;gt;Sweet&amp;lt;&amp;lt;&lt;/p&gt;
&lt;p&gt;You took the words out of my mouth...&lt;/p&gt;
&lt;p&gt;I'm stunned! In such a short time since my response, you cooked up all these codes and explanations. My hat off to you Jesse.&lt;/p&gt;
&lt;p&gt;Let me first thank Nick and yourself for going over board to answer my question. I'm a bit embarrassed and didn't ask for all this.&lt;/p&gt;
&lt;p&gt;Secondly, I have a question which I also discussed it with Nick;&lt;/p&gt;
&lt;p&gt;Here in this code which is part of the Page, you register the auc.Closed Handler:&lt;/p&gt;
&lt;p&gt;auc.Closed += new AddressUserControl.AddressEventHandler(auc_Closed);&lt;/p&gt;
&lt;p&gt;And here in the UserControl:&lt;/p&gt;
&lt;p&gt;parent.Children.Remove(this);&lt;/p&gt;
&lt;p&gt;You remove the UserControl from it's parent.children.&lt;/p&gt;
&lt;p&gt;My question is, just by removing the child from the parent, is no indication to GC to permanently &amp;quot;destroy&amp;quot; the UserControl (auc). The only way for GC to see that, is if auc=null. However, for GC to do that it has to make sure there is no reference to that object or it's internal. So shouldn't we also do the following as part of the closing?&lt;/p&gt;
&lt;p&gt;auc.Closed -= new AddressUserControl.AddressEventHandler(auc_Closed);&lt;/p&gt;
&lt;p&gt;Personally, I would remove the UserControl (auc) and setting it to null, in the parent Page when responding to Closed.&lt;/p&gt;
&lt;img src="http://blogs.silverlight.net/aggbug.aspx?PostID=51866" width="1" height="1"&gt;</description></item></channel></rss>