Loading external libraries is a very effective method to minimize the application file size. Through it’s useful, I found that it’s seldom discussed in the Internet.
I learned this a few days ago and I think that it’s important to share it immediately to al of you.
In the sample below, I have implemented a host application where it will download the assembly made from my sample “Simple Game System“. Once the assembly is downloaded, the application can initialize a a specific class from the assembly.
Comparison
Flash implementation: 30 minutes
Silverlight implementation: 20 minutes (Implemented First)
What’s the difference?
- LoaderContext [AS3] vs AssemblyPart [C#]
Source codes
Loading Assembly [Flash 9, AS3] (20.7 KiB, 986 hits)
Loading Assembly [Silverlight 2, C#] (40 KiB, 1,821 hits)
Flash
Silverlight
LoaderContext [AS3] vs AssemblyPart [C#]
In Flash, there is no dll file. However, once a SWF file is complied, you can actually utilize the resources inside the swf file. Here is a simple demonstration.
// AS3
var _assembly:Loader = new Loader();
var _loaderContext:LoaderContext = new LoaderContext();
// download the swf file
function download():void{
var request : URLRequest = new URLRequest( SWF_PATH);
_assembly.contentLoaderInfo.addEventListener(Event.COMPLETE, on_download_completed);
_assembly.load(request, _loaderContext);
}
// create an instance from the resources
function on_download_completed(e:Event):void{
var className : Class = _loaderContext.applicationDomain.getDefinition(CLASS_NAME) as Class;
var displayObject : DisplayObject = new className();
addChild(displayObject);
}
The approach in Silverlight is different. We are not going to download xap file as the assembly. Instead, will need to use the dll file generated form a project as the assembly resources.
// C#
WebClient downloader = new WebClient();
downloader.OpenReadCompleted += new OpenReadCompletedEventHandler(onDownloadCompleted);
downloader.OpenReadAsync(new Uri(DLL_PATH, UriKind.Absolute));
// Once the assembly is downloaded
private void onDownloadCompleted(object o, OpenReadCompletedEventArgs args)
{
try
{
AssemblyPart ap = new AssemblyPart();
Assembly assembly = ap.Load(args.Result);
UserControl control = (UserControl) _assembly.CreateInstance(CLASS_NAME);
LayoutRoot.Children.Add(control);
}
catch (Exception e){}
}

December 17th, 2008 at 7:03 am
[...] der nuesten Reihe von Flash vs Silverlight: Loading External Assembly/Library Dynamicallywird wieder ein toller Vergleich gezogen. Leider funktioniert dies bei mir nur im IE, im Firefox [...]
December 17th, 2008 at 6:40 pm
Hi Terence,
Unfortunately the Flash demo doesn’t work for me. I have WIndows 7 and tested IE8 beta and FF 3. No results at all when I click. The Silverlight version works just fine though.
Cheers,
Laurent
December 17th, 2008 at 10:57 pm
The Flash version did not work for me either. Silverlight version works fine.
December 17th, 2008 at 10:58 pm
@Laurent,
Seems like if you click on the bottom part of the flash object it does work with firefox 3. It doesnt work when you click on the text.
Good luck,
Rick
December 18th, 2008 at 10:38 pm
Hi,
Prefered approach is to dynmically load xap file instead of dll.
1. It would be smaller
2. Often assembly with user controls are dependent on other assemblies, so we need download them all.
Cheers,
Zakharov Alexey.
December 19th, 2008 at 2:16 pm
Alexey, Thanks for the suggestion.
January 16th, 2009 at 6:58 pm
A useful videocast about “Loading Dynamic XAPs and Assemblies”
http://silverlight.net/learn/learnvideo.aspx?video=65687
February 26th, 2009 at 11:53 pm
The flash demo uses the background as the hit area. By default in AS3 textfields and other displayobjects block events from trickling down through the displayobject tree. To allow clickthrough of the textfield the parameter ‘mouseEnabled’ should be set to false for the textfield and ’selectable’ should be set to false as well.
August 6th, 2009 at 12:10 pm
[...] Loading external libraries is a very effective method to minimize the application file size. You need to use the reflection to create obejct dynamically as well. Original Post [...]
September 27th, 2009 at 3:22 am
[...] Terence Tsang demonstrated how to load an external libraries into your own Silverlight application. This is a very effective method to minimize the application file size. [...]
October 14th, 2011 at 4:06 pm
sex
erotic
erotica
erotics
porn
sexy
celebrity
jailbait
nude
blowjob
tits
dick
cock
pussy
ebony
pussies
dicks
bukkake
war
wars
politics
obama
barack obama
kim kardashian
paris hilton
justin timberlake
justin bieber
britney spears
lady gaga
lady gagga
celebrity
cars
car
real estate
fuck
ass
anal