Skip to main content

Microsoft Silverlight

Answered Question OnApplyTemplate issueRSS Feed

(0)

GA30
GA30

Member

Member

19 points

41 Posts

OnApplyTemplate issue

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

this.Loaded += (o, e) =>{

//the Start method is the one that occassionally bombs 

this.Dispatcher.BeginInvoke(() => this.Start());

};

 

 

lingbing
lingbing

Contributor

Contributor

2249 points

406 Posts

Re: OnApplyTemplate issue

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.

Ling Bing
Bei Jing University of Aeronautics and Astronautics
Bei Jing, China

Yi-Lun Luo - MSFT
Yi-Lun L...

All-Star

All-Star

25052 points

2,747 Posts

Answered Question

Re: OnApplyTemplate issue

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.

shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.

R3al1ty
R3al1ty

Member

Member

185 points

72 Posts

Re: Re: OnApplyTemplate issue

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)

R3al1ty
R3al1ty

Member

Member

185 points

72 Posts

Re: Re: OnApplyTemplate issue

-- Double Post

GA30
GA30

Member

Member

19 points

41 Posts

Re: OnApplyTemplate issue

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

RobertMcCarter
RobertMc...

Member

Member

37 points

24 Posts

Re: OnApplyTemplate issue

 See also: http://forums.silverlight.net/forums/p/40306/327495.aspx#327495

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities