Skip to main content

Microsoft Silverlight

Event model and initialization with custom controlsRSS Feed

(0)

jpsscott
jpsscott

Member

Member

51 points

84 Posts

Event model and initialization with custom controls

Hi,

 I've worked on a few custom controls now and noticed a fairly annoying trend:

Lets say I have a control that renders its contents when I call a Load function.

When I call the controls Load function from the Load event of a user control the custom control is used in I will usually run into problems because it seems that OnApplyTemplate hasn't been called yet in the chain of events.

If a user control is loaded, why can't I access the functionality of my custom control?  Am I not wiring something up right in the custom control?

Basically I have some markup in generic.xaml, set the DefaultStyleKey and then in OnApplyTemplate I set a few member variables to controls I use within the custom control.  But OnApplyTemplate doesn't seem to be called until after my user control containing the control is loaded.

So now I've got code littered throughout my custom controls saying: if (someVar != null) {AdjustUI();}

And then I've got code in LayoutUpdated etc to do the same thing.

There has got to be a better way to do this.

Comments anyone?

James

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

All-Star

All-Star

25052 points

2,747 Posts

Re: Event model and initialization with custom controls

Unfortunately this is a limitation in Silverlight 2. Once we tried to fix the lifecycle problem but ran into a lot of browser related issues... Maybe the behavior will change in a future version, but I can't assure you anything at this time... A workaround for template is to call ApplyTemplate to force the template to be applied. But if you have problems related to layout, there's no good workarounds...

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.

jpsscott
jpsscott

Member

Member

51 points

84 Posts

Re: Event model and initialization with custom controls

OK, thanks.

 I'm putting ApplyTemplate in my constructor and I'll see how that goes.  Is there a better spot to put it?

The other thing I'm surprised about is I haven't seen this mentioned on any other examples...

 Regardless, thanks for the help and the additional info!

James

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities