Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit OnApplyTemplate issue
6 replies. Latest Post by RobertMcCarter on November 27, 2009.
(0)
GA30
Member
19 points
41 Posts
09-16-2008 2:29 PM |
Hello All!
I've got this progress bar type control that I got from around here. It works great. The only thing is that every so often the control bombs because it has code that tries to access its parts but for whatever reason on occassion the part references are null. They are null because on occassion the code that accesses the parts runs BEFORE OnApplyTemplate is called. Again, this does not always happen, only some times but enough to be a problem. I have made sure that this code that accesses the control parts does not fire until the control's loaded event fires. Furthermore, because of various things I've read out there I have taken yet extra precautions to prevent this problem by calling BeginInvoke (see code below) within the loaded event handler but still on occassion it bombs. In order to get around the problem I find myself sometimes creating timers on the fly with very short time spans which I hope will be enough time for the control's OnApplyTemplate method to be called. Anyone have any ideas?????? Thanks in advance
};
lingbing
Contributor
2249 points
406 Posts
09-16-2008 10:17 PM |
Hi, I have tested silverlight application and WPF application some days ago, and I find there is a difference. In SL application, the Loaded event fires before OnApplyTemplate method is called (as you said, sometimes it is not, I find only when a usercontrol with template is used as a control's content, the Loaded event fires after OnApplyTemplate method is called), however, in WPF, the Loaded event always fires after OnApplyTemplate method is called. I asked this question in this forum but sadly nobody answer my question.
I find a solution that works but I think this method is not good. The solution is that we can call ApplyTemplate method after the Loaded event fires, and this method will call OnApplyTemplate, after that all of the template parts will be initialized, so they will not throw NullReferenceException.
But this solution is not good because it call ApplyTemplate manually but not waiting the system to call it, I don't know if it will cause another exception, I have tested much, it works well. So before MS give us a good way to solve this problem, I will use it.
Yi-Lun L...
All-Star
25052 points
2,747 Posts
09-18-2008 3:24 AM |
Hello, this is a known issue. But it's quite complex. It affects other events such as LayoutUpdated as well. Currently the order of those events are different in Silverlight and WPF. We're investigating this issue, but I can't give you any promise at this time... In the meanwhile, you have to manually call ApplyTemplate in the Loaded event handler.
R3al1ty
185 points
72 Posts
10-02-2008 3:14 AM |
Hi Yi-Lun, any side-effects of calling ApplyTemplate in the constructor? It doesnt seem to work all the time and sometimes I am unable to get the template child out of the control (trying to isolate the specific scenario)
10-02-2008 3:19 AM |
-- Double Post
10-02-2008 9:55 AM |
Hi Yi-Lun Luo,
Do you know if this issue has been fixed with RC0? Can we now count on ApplyTemplate always being called before Loaded? Thanks for your insight
RobertMc...
37 points
24 Posts
11-27-2009 10:21 AM |
See also: http://forums.silverlight.net/forums/p/40306/327495.aspx#327495