Articles → Silverlight → On Demand Assembly Loading In Silverlight
On Demand Assembly Loading In Silverlight
Software Requirement
- Visual studio 2008 (or above) is installed on your machine.
- Silverlight (4 or 5) runtime is installed on your machine.
- Silverlight plug in for browser.
Prerequisite Knowledge
- Basic knowledge of programming in C#.
- Basic knowledge of programming in Silverlight.
- How to create .net projects(Silverlight as well as non Silverlight)
What Is On Demand Assembly Loading?
Steps Of Implementation
- Create a new Silverlight application.
- Add a custom control project in the solution.
- Set the output path of custom control project.
- Add reference of custom control project in Silverlight application.
- Use WebClient class to load the control at run time.
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
- Right click on the project and click properties.
- Property tab appears. In the ‘Build’ tab set the output path to the ‘ClientBin’ folder of the web application
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