Currently i am working on a project where i had a requirement to load multiple xap files in a silverlight application, or let me keep this way i need to load xap files OnDemand.
I was going through this article on msdn where it explains about "how to load assemblies on demand", before reading this article i really had no idea of how to load multiple xap files, but now i have done that yipee... :)
I just want to blog a bit about how to load multiple xap files in silvelright application, and also share some sample application which demonstrates this. As usual you can find the source code the end of this blog post.
To get started with this sample i've created a silverlight project and added two silverlight class libraries, now my solution looks as shown below
First let us check out how to load a single xap file, the below code shows how to read a xap file and read the dll file for the perticular class for which we want to create the instance and returns that perticular instance.
The Things to check out in the below code snippet are the two private variables:
applicationName - The name of the xap file with out extension.
control - The class in the above mentioned application for which you want to create instance.
public partial class MainPage : UserControl
{
string applicationName = "SilverlightApplication1";
string control = "Control";
public MainPage()
{
InitializeComponent();
WebClient wc = new WebClient();
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
wc.OpenReadAsync(new Uri("SilverlightApplication1.xap", UriKind.Relative));
}
void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
StreamResourceInfo resourceInfoDLL = Application.GetResourceStream(new StreamResourceInfo(e.Result, null), new Uri(string.Format("{0}.dll", applicationName), UriKind.Relative));
AssemblyPart assemblyPart = new AssemblyPart();
Assembly assembly = assemblyPart.Load(resourceInfoDLL.Stream);
UIElement element = assembly.CreateInstance(string.Format("{0}.{1}", applicationName, control)) as UIElement;
LayoutRoot.Children.Add(element);
}
}
Read more: Dot + Net <=> Digging .Net
I was going through this article on msdn where it explains about "how to load assemblies on demand", before reading this article i really had no idea of how to load multiple xap files, but now i have done that yipee... :)
I just want to blog a bit about how to load multiple xap files in silvelright application, and also share some sample application which demonstrates this. As usual you can find the source code the end of this blog post.
To get started with this sample i've created a silverlight project and added two silverlight class libraries, now my solution looks as shown below
First let us check out how to load a single xap file, the below code shows how to read a xap file and read the dll file for the perticular class for which we want to create the instance and returns that perticular instance.
The Things to check out in the below code snippet are the two private variables:
applicationName - The name of the xap file with out extension.
control - The class in the above mentioned application for which you want to create instance.
public partial class MainPage : UserControl
{
string applicationName = "SilverlightApplication1";
string control = "Control";
public MainPage()
{
InitializeComponent();
WebClient wc = new WebClient();
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
wc.OpenReadAsync(new Uri("SilverlightApplication1.xap", UriKind.Relative));
}
void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
StreamResourceInfo resourceInfoDLL = Application.GetResourceStream(new StreamResourceInfo(e.Result, null), new Uri(string.Format("{0}.dll", applicationName), UriKind.Relative));
AssemblyPart assemblyPart = new AssemblyPart();
Assembly assembly = assemblyPart.Load(resourceInfoDLL.Stream);
UIElement element = assembly.CreateInstance(string.Format("{0}.{1}", applicationName, control)) as UIElement;
LayoutRoot.Children.Add(element);
}
}
Read more: Dot + Net <=> Digging .Net