Articles → Silverlight → On Demand Assembly Loading In Silverlight

On Demand Assembly Loading In Silverlight






Software Requirement




  1. Visual studio 2008 (or above) is installed on your machine.
  2. Silverlight (4 or 5) runtime is installed on your machine.
  3. Silverlight plug in for browser.

Prerequisite Knowledge




  1. Basic knowledge of programming in C#.
  2. Basic knowledge of programming in Silverlight.
  3. How to create .net projects(Silverlight as well as non Silverlight)

What Is On Demand Assembly Loading?





Steps Of Implementation




  1. Create a new Silverlight application.
  2. Add a custom control project in the solution.
  3. Set the output path of custom control project.
  4. Add reference of custom control project in Silverlight application.
  5. Use WebClient class to load the control at run time.



Create A New Silverlight Project




Picture showing the project structure of the silverlight application
Click to Enlarge


Add Silverlight Class Library Project In The Solution




Picture showing adding a class library project in the silverlight project
Click to Enlarge


Add A Silverlight User Control In The Library Project




Picture showing adding the user control in the class 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




  1. Right click on the project and click properties.
  2. Property tab appears. In the ‘Build’ tab set the output path to the ‘ClientBin’ folder of the web application
  3. Picture showing setting the output path of the library project
    Click to Enlarge



Add A Reference Of Library Project In Silverlight Application




Picture showing adding the reference of class library project in the 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


Picture showing the output of the On demand assembly loading in silverlight
Click to Enlarge



Picture showing the output of the On demand assembly loading in silverlight
Click to Enlarge


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

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250