Articles → Silverlight → On demand assembly loading in silverlight

On demand assembly loading in silverlight






























Software Requirement











Prerequisite Knowledge











What is on demand assembly loading?





Steps of implementation

















Create a new Silverlight project





Click to Enlarge


Add Silverlight class library project in the solution





Click to Enlarge


Add a Silverlight user control in the library project





Click to Enlarge


Add code in Silverlight user control






<Grid x:Name="LayoutRoot" >
	<Border BorderThickness="5" CornerRadius="12" Width="500" Height="200" BorderBrush="DarkRed"  Background="BurlyWood" >
		<StackPanel Orientation="Vertical" >
			<TextBlock Text="Feedback Form"></TextBlock>
			<TextBlock Text="Date :" x:Name="txtLoaded"></TextBlock>
			<TextBlock Text="Enter Your Feedback Please" x:Name="lblFeedback"></TextBlock>
			<TextBox x:Name="txtFeedback" Width="Auto" Height="Auto" TextWrapping="Wrap"></TextBox>
			<Button Content="Submit" Height="23" Name="btnSubmit" Width="75" />
		</StackPanel>
	</Border>
</Grid>




public LoadOnDemandCustomControl() {
  InitializeComponent();
  this.Loaded += new RoutedEventHandler(LoadOnDemandCustomControl_Loaded);
}
void LoadOnDemandCustomControl_Loaded(object sender, RoutedEventArgs e) {
  txtLoaded.Text += DateTime.Now.ToString();
}



Set the output path of library project









Click to Enlarge


Add a reference of library project in Silverlight application





Click to Enlarge


Add code in Silverlight application






<Grid x:Name="LayoutRoot">
	<StackPanel x:Name="stkControls" Orientation="Vertical" Width="Auto" HorizontalAlignment="Center" VerticalAlignment="Center" >
		<Button Content="Click here for feedback" x:Name="btnLoad" Click="btnLoad_Click"></Button>
	</StackPanel>
</Grid>




private bool customControlsLoaded = false;

private void btnLoad_Click(object sender, RoutedEventArgs e) {
  if (!customControlsLoaded) {
    WebClient wc = new WebClient();
    wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
    wc.OpenReadAsync(new Uri("OnDemand.dll", UriKind.Relative));
  } else
    AddCustomControl();
}

void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) {
  if (e.Error == null && e.Result != null) {
    AssemblyPart part = new AssemblyPart();
    part.Load(e.Result);
    customControlsLoaded = true;
    AddCustomControl();
  }
}

private void AddCustomControl() {
  OnDemand.LoadOnDemandCustomControl ctrl = new OnDemand.LoadOnDemandCustomControl();
  stkControls.Children.Add(ctrl);
  this.btnLoad.Visibility = System.Windows.Visibility.Collapsed;
}





Output



Click to Enlarge




Click to Enlarge


Posted By  -  Ritesh Arya
 
Posted On  -  Monday, September 3, 2012

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250