Page view counter
Event model and initialization with custom controls Subscribe to this thread
Last post 10-09-2008 7:23 AM by jpsscott. 2 replies.
Sort Posts:
10-07-2008 3:31 PM
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

jpsscott

Loading...
Joined on 03-10-2008
Posts 82
10-09-2008 3:34 AM
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.

Yi-Lun Luo - MSFT

Loading...
Joined on 10-29-2007
Posts 2,747
10-09-2008 7:23 AM
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

jpsscott

Loading...
Joined on 03-10-2008
Posts 82
Microsoft Communities