Pencake Christmas Ecard - Free Online Greeting Card Silverlight vs Flash: Image Rotator
Dec 17

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

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){}
}

Shares and Enjoy~

Did you like this post?

Subscribe here:  

10 Responses to “Flash vs Silverlight: Loading External Assembly/Library Dynamically”

  1. Silverlight vs Flash: Loading external Libary at Programming with Silverlight, WPF & .NET Says:

    [...] 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 [...]

  2. Laurent Says:

    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

  3. MIchael Washington Says:

    The Flash version did not work for me either. Silverlight version works fine.

  4. Rick Says:

    @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

  5. Alexey Zakharov Says:

    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.

  6. admin Says:

    Alexey, Thanks for the suggestion.

  7. Tiago Andrade e Silva Says:

    A useful videocast about “Loading Dynamic XAPs and Assemblies”
    http://silverlight.net/learn/learnvideo.aspx?video=65687

  8. Bela Korcsog Says:

    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.

  9. Silverlight Resources Directory Update (06 Aug) Says:

    [...] 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 [...]

  10. Loading external assembly | Silverlike - A Free Microsoft Silverlight 3 Directory Says:

    [...] 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. [...]

Leave a Reply