.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

Silverlight Prism Series - Part 1

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

In this article I am going to introduce you to the Prism framework in Silverlight
 


Silverlight Prism Series - part 1 :

In this article I am going to introduce you to the Prism framework in Silverlight .

Prism Framework provides a very good solution for creating Modular applications in Silverlight .

First of we need to download Prism Framework from the Microsoft site

http://www.microsoft.com/download/en/details.aspx?id=4922 .

The dlls would be downloaded as shown below :




I guess it would help if one has bit of understanding of how MEF works . Since that would make life easier .

Well lets start . Here I would demonstrate a simple working example of Prism Implementation .

Create a new Silverlight Library Project and named it as  FirstHello .

Just add a view First.xaml .


        

Create a new Module .

public class Module : IModule
{

    #region IModule Members

    private IRegionManager regionManager;
    IUnityContainer container;
    public void Initialize()
    {

        if (this.regionManager.Regions.ContainsRegionWithName("FirstHello"))
        {
            this.regionManager.Regions["FirstHello"].Add(this.container.Resolve());
        }
    }

    public Module(IRegionManager regionManager, IUnityContainer container)
    {
        this.container = container;
        this.regionManager = regionManager;
    }

    #endregion

}

The Module implements the Imodule Interface .

namespace Microsoft.Practices.Prism.Modularity
{
    // Summary:
    //  Defines the contract for the modules deployed in the application.
    public interface IModule
    {
        // Summary:
        //  Notifies the module that it has be initialized.
        void Initialize();
    }
}

Similarly lets add a another Silverlight library project .

Second.xaml is the View .


  
  
  
  

The next Module looks as :

 
public class Module1 : IModule
{
    #region IModule Members

    private IRegionManager regionManager;
    IUnityContainer container;
    public void Initialize()
    {

        if (this.regionManager.Regions.ContainsRegionWithName("SecondHello"))
        {
            this.regionManager.Regions["SecondHello"].Add(this.container.Resolve());
        }
    }

    public Module1(IRegionManager regionManager, IUnityContainer container)
    {
        this.container = container;
        this.regionManager = regionManager;
    }

    #endregion

}


Let us create a new Silverlight Project SLPrismPart1 .


Lets Create a Bootstrapper .

public class Bootstrapper : UnityBootstrapper
{
    protected override DependencyObject CreateShell()
    {
        Shell shell = new Shell();
        Application.Current.RootVisual = shell;
        return shell;
    }

    protected override IModuleCatalog CreateModuleCatalog()
    {
        ModuleCatalog modules = new ModuleCatalog();
        modules.AddModule(typeof(FirstHello.Module));
        modules.AddModule(typeof(SecondHello.Module1));
        return modules;
    }

}

The Bootstrapper class extends the UnityBootstrapper class .  The UnityBootstrapper class looks as below :
 



The Bootstrapper class looks as follows :

public class Bootstrapper : UnityBootstrapper
{
    protected override DependencyObject CreateShell()
    {
        Shell shell = new Shell();
        Application.Current.RootVisual = shell;
        return shell;
    }

    protected override IModuleCatalog CreateModuleCatalog()
    {
        ModuleCatalog modules = new ModuleCatalog();
        modules.AddModule(typeof(FirstHello.Module));
        modules.AddModule(typeof(SecondHello.Module1));
        return modules;
    }

}
The xaml Shell which would display both our Modules will look as follows :



  
  
  
  
  
  
  
  
  
   
  
    
  
  
  
  
  
  

The Application_Startup in the App.xaml will look as follows :

private void Application_Startup(object sender, StartupEventArgs e)
{
    Bootstrapper bootStrapper = new Bootstrapper();
    bootStrapper.Run();
}
We are done lets give it a run :



In my next post I would go more indepth into the Prism Framework . Thanks. Happy coding .

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

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