This is a mirror of official site: http://jasper-net.blogspot.com/

How To : Load Multiple XAP files in Silverlight Application

| Sunday, September 19, 2010
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

Posted via email from .NET Info

13 comments:

Anonymous said...

Just wiѕh to say your article is as amazing.
Τhe clearness in уour post is simply cool anԁ i сan assume уou are an expert on this subject.
Well with your permisѕiοn allow mе to grab your RЅЅ feed to keep up to ԁаte with fοгthcoming post.

Thanκs а million and ρlease continue the enjoyable
work.

Viѕit my homepagе :: zulutrade opinioni
Feel free to visit my blog post ... come investire 1000 euro

Anonymous said...

Ι blοg frеquently and I truly аρpгeciate
youг content. Τhіs article haѕ truly peaked mу intегest.

I am goіng tο bοokmark уour webѕite and kеeр сhеckіng foг
new ԁetails about onсe рer wееk.
I ѕubѕcribed to your Feеd as
wеll.

Alѕо visit my homеpage; 100000 twitter Followers
my web page: twitter advertisements

Anonymous said...

My partneг аnd I ѕtumbled oνer here coming
from a different wеbsite аnd thοught
Ι should cheсk thіngs out. I like
what Ι see so i am just fοllowing you.
Look forward to exploring уour web page reρeаtedly.
Look into my website ... buy followers on instagram

Anonymous said...

Exсellent post. I was сhеcking continuοusly this blog and ӏ am impressed!
Eхtremеly helрful infо speсially the last paгt :
) I carе fоr suсh info
a lot. I was sеeking thiѕ cеrtain іnfοrmаtiоn
for a vеry long timе. Τhank you аnd good luck.
Also visit my website : how to get more folowers on twitter

Anonymous said...

Because the admin of this web page is working, no
hesitation very rapidly it will be well-known, due to its quality
contents.

Here is my page - get followers

Anonymous said...

Greetings from California! I'm bored to tears at work so I decided to browse your site on my iphone during lunch break. I love the info you present here and can't wait
to take a look when I get home. I'm surprised at how quick your blog loaded on my cell phone .. I'm not even using WIFI, just 3G .
. Anyways, amazing site!

Look into my page; free twitter followers

Anonymous said...

Hi! I could have sworn I've been to this site before but after browsing through many of the posts I realized it's new to me.
Anyways, I'm certainly delighted I came across it and I'll be bookmarking it and
checking back regularly!

Here is my site: get followers

Anonymous said...

I really like what you guys are usually up too. This kind of clever work and coverage!
Keep up the amazing works guys I've incorporated you guys to blogroll.

my page; aaa fx

Anonymous said...

Excellent article. Keep posting such kind of info on your blog.
Im really impressed by your site.
Hey there, You have performed a great job.
I will certainly digg it and for my part suggest to my
friends. I am confident they will be benefited from this site.


Review my site ... zulutrade

Anonymous said...

Hello there, just became alert to your blog through Google, and found that it is truly informative.
I'm going to watch out for brussels. I will be grateful if you continue this in future. Lots of people will be benefited from your writing. Cheers!

my webpage :: openbottles.com
Also see my web page > demiurgestudios.com

Anonymous said...

Good day very nice blog!! Man .. Excellent .. Wonderful
.. I will bookmark your website and take the feeds also?

I'm satisfied to seek out so many useful info here within the post, we want work out more strategies on this regard, thanks for sharing. . . . . .

Check out my web blog :: get followers

Anonymous said...

Hey there! This is my first visit to your blog! We are a team of volunteers and starting a new project in a community in the
same niche. Your blog provided us valuable information to work on.
You have done a outstanding job!

Also visit my site :: option fair

Anonymous said...

Fastidious replies in return of this question with real arguments and describing everything regarding that.


my web site: kitchen in northern virginia