Up Advanced topics
DevForce Resource Center » Punch » Advanced topics » Dynamic loading of XAP files

Dynamic loading of XAP files

Last modified on January 26, 2015 13:56

In Silverlight, Punch provides the means to dynamically load XAP files. All MEF Exports found in the assemblies contained in the XAP file will be added to the MEF container and can subsequently be used in the application. In addition, any XAP file can contain additional DevForce models that will also be available for use after the loading completes.


How to dynamically load a XAP file

The static Composition class provides the necessary support for dynamically loading XAP files through the AddXapAsync() method.

The following example demonstrates dynamically loading a ViewModel from a secondary XAP file upon receiving a message from the Caliburn Micro Event Aggregator. Notice that loading a XAP file is an asynchronous operation.

C#
[Export(typeof(IMessageProcessor))]
[PartCreationPolicy(CreationPolicy.Shared)]
public class EditOrderMessageProcessor : IMessageProcessor, IHandle<EditOrderMessage>
{
   public EditOrderMessageProcessor()
    {
        EventFns.Subscribe(this);
    }

   public async void Handle(EditOrderMessage message)
    {
       // Let's make sure the xap that contains the OrderEditor is loaded.
       // Only the first call will load the xap, subsequent calls don't do anything.
       await Composition.AddXapAsync("HelloWorld.Module.xap");

       // Now we can access the OrderEditorFactory. MEF recomposed this instance
       // and provided us with an OrderEditorFactory.
       var editor = OrderEditorFactory.CreateExport().Value;
        var handler = editor as IHandle<EditOrderMessage>;
       if (handler != null)
            handler.Handle(message);
    }

   /// <summary>
   /// OrderEditorFactory is not available until the xap file gets loaded. AllowDefault=true,
   /// makes sure we don't fail if MEF can't find the OrderEditor.
   /// AllowRecomposition=true, will tell MEF to set the OrderEditorFactory as
   /// soon as it is available after loading the xap.
   /// </summary>
   [Import("OrderEditor", AllowRecomposition = true, AllowDefault = true)]
   public ExportFactory<object> OrderEditorFactory { get; set; }
}
Tags: Cocktail
Created by DevForce on January 13, 2012 18:08

This wiki is licensed under a Creative Commons 2.0 license. XWiki Enterprise 3.2 - Documentation. Copyright © 2020 IdeaBlade