views:

53

answers:

1

I am trying to initialize my controls in Silverlight. I am looking for something similar to Form_Load event, which gets triggered when the form loads first time.

The Loaded event in Silverlight gets called quite early, even before the control gets displayed in UI. I want to intialize prior to the UI rendering of the control the first time. What choices do i have? Below is my code for reference. It's pretty basic.

Appreciate your response!

1    <local:ControlBase x:Class="MyUserControl"
2        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
3        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
4        xmlns:local="clr-namespace:GridTest.UI.Framework;assembly=GridTest.UI.Framework"
5        xmlns:ImageViewer="clr-namespace:GridTest.ImageViewer"
6        FontFamily="./Fonts/CALIBRI.TTF#Calibri" FontSize="13">
7        <Grid x:Name="LayoutRoot" Background="White" Loaded="MyUserControl_Loaded" >
8            <ImageViewer:ImagePreview HorizontalAlignment="Stretch" VerticalAlignment="Stretch" x:Name="ucImagePreview"/>
9    </local:ControlBase>
10
+1  A: 

For this scenario I tend to wire up the UserControls Loaded property in the UserControl constructor and have not come across any issues with this.

So in my control code behind I have:

public Control()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Control_Loaded);
        }

void Control_Loaded(object sender, RoutedEventArgs re)
        {
//Do any init here
}

HTH

Andy Britcliffe