.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Loading Xaps On demand in Silverlight ( MEF )

Posted By:Mahadesh Mahalingappa       Posted Date: August 06, 2011    Points: 200    Category: Silverlight    URL: http://www.dotnetspark.com  

Was reading about loading Multiple Xaps in Silverlight . This is very usefull feature as you can create dynamic content very easily .
 

I was reading about loading Multiple XAPs in Silverlight. This is a very usefull feature since it makes creation of dynamic content very easy.

I checked out the article http://blog.weareon.net/how-to-load-multiple-xap-files-in-silverlight-application/ 

Let us extend this article using MEF.

Also it would be good if you can go through the MSDN article on Downloading content on Demand http://msdn.microsoft.com/en-us/library/cc189021(v=vs.95).aspx 


What I have done with this article is simply add Mef to it so that we can better control how we want to load our XAP.

Let me just walk through the steps which I have borrowed from Kranthi Gullapalli in his article. Thanks Kranthi for your help.

Create a new Silverlight project as MultipleXaps. Let us choose the Silverlight version as 4. Do make sure to choose a web project to deploy our silverlight project.

Make sure you add references for the following DLLs. These are necessary for Meffing. The solution should look as below:

XAPS On demand in Silverlight

The following is the code for MainPage.xaml.cs.

public partial class MainPage : UserControl
{

    public MainPage()
    {
        InitializeComponent();
        WebClient wc = new WebClient();
        wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
            wc.OpenReadAsync(new Uri(cls.xap, UriKind.Relative));
        }
        void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            UIElement element = GetUIElementFromXap(e, cls.applicationName, cls.control);
            LayoutRoot.Children.Add(element);
        }
        private static UIElement GetUIElementFromXap(OpenReadCompletedEventArgs e, string applicationName, string control)
        {
            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;
            return element;
        }
}

Let us break down the above program:

1. Let's dig into the constructor.

public MainPage()
{
    InitializeComponent();
// Create an instance of WebClient â?" The WebClient Provides common methods for sending data to and receiving data from a resource identified by a URI. 

    WebClient wc = new WebClient();

 //  OpenReadCompleted Event -Occurs when an asynchronous resource-read operation is completed.

    wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);

// In the below step we call the method asynchronously as we do usually in Silverlight  - Note we pass the xap name and its relative path in this method .

   wc.OpenReadAsync(new Uri(cls.xap, UriKind.Relative));
 
}

2. Now let's check out the method GetUIElementFromXap â?" this is a custom method to return the UIElement from the XAP.

Note the parameters which are passed to the method:

  1. OpenReadCompletedEventArgs:
  2. ApplicationName - Which would be the application name in the XAP we are trying to load on demand.
  3. Control - Control from the XAP we are trying to load. 
     
private static UIElement GetUIElementFromXap(OpenReadCompletedEventArgs e, string applicationName, string control)
{
 

// In the below line of Code I am passing all the three parameters to create an instance of StreamResourceInfo  which Stores information for a stream resource .

            StreamResourceInfo resourceInfoDLL = Application.GetResourceStream(new StreamResourceInfo(e.Result, null),
                                                 new 
Uri(string.Format("{0}.dll", applicationName), UriKind.Relative));
 
// An assembly part is an assembly that is to be included in a Silverlight-based application package (.xap).

            AssemblyPart assemblyPart = new AssemblyPart();
// Converts a Stream to an Assembly that is subsequently loaded into the current application domain.

            Assembly assembly = assemblyPart.Load(resourceInfoDLL.Stream);

// Once the Assembly is loaded we can get the UIElement out of it by Creating a Instance and passing the applicationName and Control Name to it .

            UIElement element = assembly.CreateInstance(string.Format("{0}.{1}", applicationName, control)) as UIElement;
 
// Finally return the UIElement .
 
            return element;
}


Creating the Plugin to be loaded on Demand:

We can now go ahead and create a simple Plugin as shown below. I just name it as SilverlightApplication1.

XAPS On demand in Silverlight

The XAML code would look like as shown below:


    
       
              
       
    


Creating the Plugin Manager:

I just created a Silverlight Library Project and named it Plugin Manager. Create a class class1. The code for the class looks as below:

[Export]
    public class Class1
    {
        public string xap { get; set; }

        public string applicationName { get; set; }

        public string control { get; set; }

        [ImportingConstructor]
        public Class1()
        {

            xap = "SilverlightApplication1.xap";
 
            applicationName = "SilverlightApplication1";
 
            control = "Control";
        }
    }


Add the Library Reference to our MultipleXaps project.


Modify the MainPage.xaml.cs in the MultipleXaps project to enable MEF:

 

 public partial class MainPage : UserControl
    {
        [Import]
        public Class1 cls { get; set; }

        public MainPage()
        {
            InitializeComponent();
 
            CompositionInitializer.SatisfyImports(this);

            WebClient wc = new WebClient();
            wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
            wc.OpenReadAsync(new Uri(cls.xap, UriKind.Relative));

        }

        void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            UIElement element = GetUIElementFromXap(e, cls.applicationName, cls.control);
            LayoutRoot.Children.Add(element);
        }

        private static UIElement GetUIElementFromXap(OpenReadCompletedEventArgs e, string applicationName, string control)
        {
            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;
            return element;
        }

    }

Once we have set up MEF in our application we can simply go ahead and give it a run.

XAPS On demand in Silverlight

Hence our Plugin gets loaded successfully.

Happy Coding.
 


 Subscribe to Articles

     

Further Readings:

Responses
Author: RubberTramp         Company URL: http://www.dotnetspark.com
Posted Date: March 07, 2012

Thanks Mahadesh for this Nice article.
I have the following quesiton.
How can I pass parameters to the dynamically loaded XAP?

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend