(X) Hide this
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .

Working with Prism 4 Part 1: Getting Started

(7 votes)
Brian Noyes
>
Brian Noyes
Joined Jun 10, 2010
Articles:   19
Comments:   117
More Articles
41 comments   /   posted on Nov 07, 2011
Tags:   prism , brian-noyes

Tweet

This is Part 1 in the series Working with Prism 4.

Introduction

Prism is a toolkit produced by Microsoft patterns & practices to aid you in building Silverlight and WPF composite applications.

Now, that probably doesn’t tell you much unless you have been building composite applications with other toolkits or frameworks and know what that term really means, so let me expand on what that means and what the real purpose of Prism is to get started.

A composite application can be thought of as the opposite of a monolithic application. I can hear you thinking now: “Ah, now I see. I know monolithic applications are bad, therefore composite applications must be good. Yin/Yang, Dark/Light. I need to check this out!” Or at least that is what I hope you are thinking. Composite applications are built from more granular, loosely coupled parts. Two of the primary reasons for approaching application development in a composite way are for maintainability and extensibility. If your application is composed of fine grained, well-focused, loosely coupled parts, it should be easier to develop and easier to maintain and add new functionality over time. Things like the code that defines the structure of your user interface (XAML in Silverlight, WPF, and Metro), the code that supports the interaction logic with the user (view models in the MVVM pattern), the data structures and logic (the model in MVVM), and services that provide shared functionality for service or data access in your application are examples of things that are better separated into parts rather than getting all woven together in spaghetti code. The fact that building composite applications will help you with maintainability is a little hard to prove up front, but the over 50,000 downloads of Prism 4 should tell you that there are a lot of companies out there that get this and like Prism as a toolkit for building apps that way.

To get more specific, Prism has a number of specific feature sets that I will explore in this series that helps you to build your client application as a composite application. These include:

  • Modularity – Functionality to define and dynamically load chunks of loosely coupled functionality into a single running application instance.
  • UI Composition – Functionality to plug in views into parent containers in a loosely coupled fashion where the parent and child do not need to know explicitly about one another with direct object references.
  • Communications – Functionality to support loosely coupled commands and pub/sub events between the components of your application.
  • Navigation – Functionality to switch views when the user interacts with the application within a container without every view and parent view needing to know about all the others.
  • Application Structure – Support for the Model-View-ViewModel pattern, as well as a dependency injection container, and other implementation patterns to help set up your solution for the best separation of concerns for different kinds of code in your application.

An important thing to understand about Prism is that it is not an all-or-nothing framework. You can use any one or several of the features in isolation and ignore the other parts if they do not make sense for your application or your requirements. In addition to these major features, there are many small little helper classes and utilities in Prism that can be used on their own as well.

In the rest of this article, I will quickly step you through creating a simple Prism application that uses modularity and regions to plug a view into the main application. You will see that there are a number of steps that may seem a bit complicated at first. The first thing to keep in mind is that these activities are a one-time set up of your solution as you lay down the structure of your solution. The other is that at the end of the article I will point you to some project templates that make it all much quicker. But I think it is important to go through the steps manually at least once so you understand what all the moving parts are. In the next article, I will flesh that application out some more to use commands and events and later articles will explore other aspects of Prism.

Download the source code for the Prism 101 completed solution.

Step 1: Create your Prism application

To get started, create a new Silverlight Application project in Visual Studio and name it Prism101.

Figure1

In the New Silverlight Application dialog that asks about creating a host application, accept the defaults by clicking OK. (Note: I am using Silverlight 5 RC for my example, but all of what I will show works equally well for Silverlight 4).

Figure2

Step 2: Decide which Dependency Injection container to use

Prism uses the Inversion of Control and Dependency Injection patterns under the covers with the help of a Container. Out of the box, Prism supports using either Unity or Managed Extensibility Framework (MEF). You can also plug in other containers by implementing the IServiceLocator interface similar to the implementations that are provided for Unity and MEF.

I won’t go into a full discussion of the differences and similarities between Unity and MEF. Each has distinct capabilities, but for the purposes of what I will be covering in this article series, either one is equally sufficient. I generally prefer MEF myself since it is a part of the .NET and Silverlight Frameworks, and because I like the declarative approach that MEF uses. So I will be using MEF throughout the article series. The sample applications that come with Prism have numerous examples of using Unity instead.

Step 3: Add the Prism Library assemblies

To follow along with the coding, you will need to download the Prism 4 toolkit. It is a self-extracting executable that you can point to any folder on your machine where you want to store the libraries, source code, sample code, and documentation for Prism 4. Once you have extracted the files, add references to the Prism101 Silverlight client application project to the Microsoft.Practices.Prism, Microsoft.Practices.Prism.Interactivity, and Microsoft.Practices.Prism.MefExtensions libraries in the \Bin\Silverlight folder wherever you extracted Prism to:

Figure3

For this sample article I will not be using any functionality from the Interactivity library, but it is easier to just always add references to all three of these libraries in your client projects so that you have the full Prism toolkit at your disposal. If you were using Unity instead of MEF, you can see that instead of adding the MefExtensions library, you would add the UnityExtensions  and Unity.Silverlight libraries instead

Also add references to the System.ComponentModel.Composition, System.ComponentModel.Composition.Initialization and the System.Windows.Controls libraries.

Step 4: Add a Bootstrapper

If you are going to use modularity, UI composition, or navigation in your application, you will need to have some Prism-specific initialization code in your application. The standard way to set up this initialization code is to have it in a separate class called a bootstrapper. The bootstrapper is just a way to centralize all the initialization logic for your application in one well known location, separate from the App.xaml code behind. Prism provides a base bootstrapper class for each of the containers that takes care of initializing all the types and services in the Prism toolkit. All you need to do is derive from it and handle a couple of overrides to initialize the specifics of your application.

Add a class named Bootstrapper to your Prism101 project. Modify the code to match the code below.

 1: public class Bootstrapper : MefBootstrapper
 2: {
 3:     protected override DependencyObject CreateShell()
 4:     {
 5:         MainPage shell = new MainPage();
 6:         Application.Current.RootVisual = shell;
 7:         return shell;
 8:     }
 9: }

Part of the initialization of the application is to create the main page. The outermost UI (Window in WPF or RootVisual in Silverlight) is referred to as the Shell in a Prism application. Part of the design of a composite application is that the shell is just a place for content to live. The specific content and functionality of your application will be contributed by the modules that load into the shell. I will get to creating a module later in the article. The shell is responsible for setting up the overall look and feel of the application (i.e. contributing the application scoped resources), determining the top level window layout and navigation. However, the shell should remain ignorant of the details of what is going to load into the application other than those aspects.

So the code above just initializes the application’s RootVisual to an instance of the MainPage class and returns a reference for that instance to the bootstrapper base class. Prism will use that reference to hook up some of the region functionality that I will be using shortly.

As you may know, normally your RootVisual is set in the App.xaml.cs file of your project, and you can only set the RootVisual once per application. Additionally, for the bootstrapper to do anything, it has to be called from somewhere. Open your App.xaml.cs file and modify the Application_Startup event handler to run the bootstrapper instead of setting the RootVisual:

 1: private void Application_Startup(object sender, StartupEventArgs e)
 2: {
 3:     new Bootstrapper().Run();
 4: }

The Run method on the MefBootstrapper base class will execute an intialization workflow that performs the following:

  • Creates the container
  • Adds MEF exports to the container for the types contributed by Prism
  • Creates and initializes the shell
  • Loads and initializes the modules

The third bullet is the point where the overridden CreateShell method in your bootstrapper will be called. The main application project (Prism101) is referred to as the Shell application project since it is the root application that loads and hosts the shell.

Build and run the application at this point to make sure all is well. Your Silverlight application should launch like normal with an empty main page, but you should not see any exceptions. There is not much there to see yet, but the addition of the bootstrapper has made it so all the functionality of Prism is now at your disposal in your application. Next I will show how to start harnessing some of that functionality by defining a module.

Step 5: Add a Module project to your solution

A Module in Prism is an abstract container for a set of functionality. The way you partition your sets of functionality into modules is completely arbitrary, but common lines of separation are to put individual feature sets that you want to be able to turn on or off as a whole in the application into separate modules. Another common approach is to separate modules based on team composition and which team will be working on which set of functionality. For this article, I will just add a single Core module with the main view that will load up to welcome the user and get them started using the application. In later articles I will add additional modules and show how you can plug functionality from multiple modules in at the shell level, or can even plug functionality into the views of one module from another module.

Add another Silverlight Application project to your solution and name it Prism101.Modules.Core.

Figure4

When the New Silverlight Application dialog pops up, uncheck the option at the top to host the application and click OK.

Figure5

Normally you only create a Silverlight Application project type for your main application, and any types that you separate into other assemblies you would create with a Silverlight Class Library project type. A module in Prism is an independently loadable unit of functionality. By creating it as a Silverlight Application project, it will get compiled into its own XAP file and can be downloaded and loaded independently from the rest of the application. That is part of the decoupling you are going after when you develop an application as a composite application.

Because this project will not launch as a standalone Silverlight Application itself though, delete the MainPage.xaml and App.xaml files from the project. Then go to the project properties and change the Startup object drop down to (not set).

Figure6

Step 6: Add the Module class

The thing that identifies an assembly as a module to Prism is that it contains one or more module classes. A module class is just a class that implements the Prism IModule interface. When using MEF as the container, you also have to decorate the class with a [ModuleExport] attribute so that Prism can discover the module class through the container. Even though Prism supports defining more than one module class in a single assembly, my design recommendation is to only ever have one module class for a given module project. That allows you to treat the assembly boundary as the container for a single module’s functionality rather than trying to rationalize which types within the assembly are associated with which logical module.

Before you add the module class, you will need the same references for each module that you added to the shell project:

  • Microsoft.Practices.Prism
  • Microsoft.Practices.Prism.Interactivity
  • Microsoft.Practices.Prism.MefExtensions
  • System.ComponentModel.Composition
  • System.ComponentModel.Composition.Initialization
  • System.Windows.Controls

However, one important difference is that you need to set the Copy Local property for each of the Prism references to false so that those assemblies do not get copied into the application multiple times. They will be present at runtime because the Shell project will already have them in its XAP file. Having multiple copies load up causes problems in the module loading process.

Add a class to the Prism101.Modules.Core project and name it CoreModule. Add code to the class so it matches the following code snippet:

 1: [ModuleExport(typeof(CoreModule))]
 2: public class CoreModule : IModule
 3: {
 4:     public void Initialize()
 5:     {
 6:         MessageBox.Show("Core Module initialized!");
 7:     }
 8: }

 

Step 7: Define the Module catalog

Prism supports several ways of loading modules into the shell application. Modules can be downloaded along with the main application or they can be downloaded asynchronously in the background. They can be loaded as soon as they have been downloaded or you can defer loading of the module until the first point where the functionality is invoked from the module. You can declare which modules the application is composed of programmatically or declaratively in a module XAML file. The module loader is also extensible, so if you wanted to write your own module catalog implementation that went out and made a service call with the logged in user identity to get back a tailored list per user of what modules to download and load, you could easily do that as well. In fact, that might make a good future article… leave a comment if you would like to see that.

For this sample, I will use a ModuleCatalog.xaml file to indicate which modules to load. For the Core module I will have it download and initialize as the application starts up since it represents the initial landing page for the user. In a later article I will show how to defer downloading and initialization until additional functionality is first invoked.

Add a Text File from the General category of project item templates named ModuleCatalog.xaml to the Prism101 project.

Figure7

Edit the contents of the file to contain the following markup.

 1: <Modularity:ModuleCatalog xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 2:                           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 3:                           xmlns:sys="clr-namespace:System;assembly=mscorlib"
 4:                           xmlns:Modularity="clr-namespace:Microsoft.Practices.Prism.Modularity;assembly=Microsoft.Practices.Prism">
 5:     <Modularity:ModuleInfo Ref="Prism101.Modules.Core.xap"
 6:                            ModuleName="CoreModule"
 7:                            ModuleType="Prism101.Modules.Core.CoreModule, Prism101.Modules.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
 8: </Modularity:ModuleCatalog>

This markup identifies the module assembly information in a declarative fashion outside the compiled code so that it could potentially be changed on the deployment site to add or remove modules without needing to recompile anything. Go to the properties for the file and make sure its Build Action is set to Resource.

Step 8: Add the module XAP to the host site

For Prism to be able to download the module XAP file, it will have to be present in the hosting web applications \ClientBin folder. To add this, open the Prism101.Web project and go to its project properties. Select the Silverlight Applications tab and Click the Add button at the bottom.

In the Add Silverlight Application dialog, select Prism101.Modules.Core from the drop down list for “Use and existing Silverlight application project in the solution”. Uncheck the option towards the bottom to “Add a test page that references the control”. Click Add.

Figure8

You should now see both the Shell project (Prism101) and the module project (Prism101.Modules.Core) listed as hosted applications.

Figure9

Step 9: Read the ModuleCatalog.xaml from the bootstrapper

The final step to get the module loaded is to modify the bootstrapper so it knows where to find the module catalog information. Add this method to your Bootstrapper class:

 1: protected override Microsoft.Practices.Prism.Modularity.IModuleCatalog CreateModuleCatalog()
 2: {
 3:     return Microsoft.Practices.Prism.Modularity.ModuleCatalog.CreateFromXaml(
 4:         new Uri("/Prism101;component/ModuleCatalog.xaml", UriKind.Relative));
 5: }

At this point you should be able to run the application again and you will see the message box pop up as the application loads, indicating that the module has in fact loaded.

Step 10: Add a Region to the Shell

Popping a message box at least tells us our module is loading and initializing, but is not very interesting. What you will typically do as a module loads up is start presenting some screens or at least commanding functionality to the user that lets them take advantage of the functionality that is encapsulated in the module. So now I will show how you can define a view in a module and present it to the user by plugging it into the shell as the module loads.

The first step in doing that is to define somewhere for the view to plug in within the shell. The most common way to do this is to add either a ContentControl or ItemsControl (or derived type) to the shell and mark it as a Prism region.

Open MainPage.xaml and add the Prism XAML namespace to the XML namespaces on the root UserControl element so that you can access types from the Prism Library in XAML

 1: xmlns:prism="http://www.codeplex.com/prism"

Then add a ContentControl and mark it as a region named MainContent:

 1: <Grid x:Name="LayoutRoot" Background="White">
 2:     <ContentControl prism:RegionManager.RegionName="MainContent" />
 3: </Grid>

 

Step 11: Add a View to the Core Module and plug it into the Shell region

Add a new Silverlight User Control to the Prism101.Modules.Core project named WelcomeView. In the XAML, add a TextBlock with its Text property set to “Hello Prism!” and set the font size big to celebrate your excitement with Prism.

 1: <Grid x:Name="LayoutRoot" Background="White">
 2:     <TextBlock Text="Hello Prism!"
 3:                 FontSize="24" />
 4: </Grid>

Next, open the CoreModule class and replace the code there with the following code.

 1: [ModuleExport(typeof(CoreModule))]
 2: public class CoreModule : IModule
 3: {
 4:     [Import]
 5:     public IRegionManager RegionManager { get; set; }
 6:  
 7:     public void Initialize()
 8:     {
 9:         var view = new WelcomeView();
 10:         RegionManager.AddToRegion("MainContent", view);
 11:     }
 12: }

The import allows your module class to access the RegionManager service exposed by Prism. This manager knows about all regions declared in any view throughout the application and allows you to dynamically plug in child views into those regions. The Initialize method code then creates an instance of your module’s view and plugs it into the MainContent region you placed in the shell as a ContentControl. To do this without Prism you would somehow have to feed an object reference to that ContentControl from the MainPage down into the module code that is loading into the application so that it could programmatically set the Content property to the view instance. That would be an example of the kind of coupling that you are trying to avoid when building composite applications. At this point the only coupling between the main application and the module are the declaration of the module in the ModuleCatalog.xaml and the shared “contract” of the MainContent region name as a string.

Step 12: Making it easier

I’m sure that seemed like a lot of work to get a Hello Prism view displayed in your application. And if you app is fairly small – say less than a couple dozen screens, you might not bother with using modularity and regions at all. In later articles in the series I’ll get into other features of Prism including commands, events, and some of the Model-View-ViewModel pattern support that can be very handy regardless of the scale of your application.

But even all this initial setup cries out for automation, and it happens to be readily available in the form of the Prism Template Pack created by David Hill, a Microsoft Program Manager from the Prism team.

If you follow the instructions at that link to download an install the template pack, you can quickly create a Prism starter app with a module following these steps:

Create a new project, select the Prism templates in the New Project dialog, and pick the MEF category. Start by selecting the Prism Silverlight 4.0 Shell (MEF) project type. This will create a pre-populated shell project with several regions already defined in the shell, the bootstrapper all set up for you, references added, etc.

Figure11

Next, add another project to the solution and this time select the Prism Silverlight 4.0 Module (MEF) project type. Keep the default name of Module1.

Next go into the ModuleCatalog.xaml in the shell project and uncomment the Module1 information:

 1: <prism:ModuleInfo Ref="Module1.xap"
 2:                     ModuleName="Module1.ModuleInit"
 3:                     ModuleType="Module1.ModuleInit, Module1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
 4:                     InitializationMode="WhenAvailable"/>

Next add a new ASP.NET Empty Web Application project to act as a host. The Readme.txt file in each project tells you how to configure it, but it is similar to what I showed in Step 8 of this article to configure the web application as the host for the Shell and to add the Module1 XAP to be hosted in the site as well for download. Then you will want to launch the app using the web project as the startup project with the test page for the Shell as the startup page. If you do that, viola – you have a running Prism app with a module and a bit more functionality than the one we put together in this article because it has some views wired up with some sample data and navigation commands and other things we will be talking about in later articles.

These templates will need to be updated for Silverlight 5 and you can customize them yourself if you want to trim them down to get rid of all the sample data and such to be a better (empty) starting point for real projects.

Download the source code for the Prism 101 completed solution.

Summary

In this article I gave you a quick introduction to the concepts and capabilities of Prism and walked you through creating your first Prism application, explaining as we went what some of the moving parts and pieces are that make a Prism application a little different than a normal Silverlight app. You saw that there is some initialization required with the bootstrapper to get the Prism services and types initialized to support your application. The bootstrapper is also where you create your main shell view as your RootVisual and where you indicate, either directly or indirectly through a module catalog XAML file, which modules you want to load into your application. You then saw how to define a module and get it initialized, as well as how to plug views into the shell with Regions. In the next article, I’ll put a little more “meat” in the views to include some commands and the MVVM pattern. In later articles I’ll expand to multiple modules, navigation, events and more.

About the Author

Brian Noyes is Chief Architect of IDesign, a Microsoft Regional Director, and Silverlight MVP. He is a frequent top rated speaker at conferences worldwide including Microsoft TechEd, DevConnections, VSLive!, DevTeach, and others. Brian worked directly on the Prism team with Microsoft patterns and practices and co-authored the book Developers Guide to Microsoft Prism4. He is also the author of Developing Applications with Windows Workflow Foundation, Smart Client Deployment with ClickOnce, and Data Binding in Windows Forms 2.0. Brian got started programming as a hobby while flying F-14 Tomcats in the U.S. Navy, later turning his passion for code into his current career. You can contact Brian through his blog at http://briannoyes.net/ or on Twitter @briannoyes.


Subscribe

Comments

  • Arterius

    Re: Working with Prism 4 Part 1: Getting Started


    posted by Arterius on Nov 08, 2011 10:36
    Thank you. Good and easy understanding article. I will wait impatiently to remaining parts. Also I would very much like articles about MVVM Light Toolkit.
  • joesimon

    Re: Working with Prism 4 Part 1: Getting Started


    posted by joesimon on Nov 09, 2011 15:18

    Thank you, good article!

    I have just started to learn Prism, so it's very usefull for me.

    "The module loader is also extensible, so if you wanted to write your own module catalog implementation that went out and made a service call with the logged in user identity to get back a tailored list per user of what modules to download and load, you could easily do that as well. In fact, that might make a good future article… leave a comment if you would like to see that."

    I'm interested in how it is possible to download modules depending user identity.

  • truyenle

    Re: Working with Prism 4 Part 1: Getting Started


    posted by truyenle on Nov 10, 2011 10:19

    Awesome article and the reference in step 12. I have been searching for this for couple of days. Looking forward to see the next article especially the " write module catalog implementation that went out and made a service call with the logged in user identity to get back a tailored list per user of what modules to download and load"

     Truyenle

  • geertd

    Re: Working with Prism 4 Part 1: Getting Started


    posted by geertd on Nov 10, 2011 12:04
    Very clear and to the point. Like it. Is there a schedule for the next parts?
  • brian.noyes

    Re: Working with Prism 4 Part 1: Getting Started


    posted by brian.noyes on Nov 10, 2011 13:13

    Planning on one article every two weeks or so. Looking at probably 4 more articles on commands and events, MVVM support, navigation with regions.sounds like one on custom modularity loading is of interest as well.

  • geertd

    Re: Working with Prism 4 Part 1: Getting Started


    posted by geertd on Nov 10, 2011 17:43

    Brian,

    Thanks for the update.

  • SbastienBrasseur

    Re: Working with Prism 4 Part 1: Getting Started


    posted by SbastienBrasseur on Nov 15, 2011 13:35
    Great Introduction! Now it sounds clear to me what is Prism and I think it is a good starting point to dive into the intricacies of Prism in the next articles you will provide!
  • tle

    Re: Working with Prism 4 Part 1: Getting Started


    posted by tle on Nov 19, 2011 09:12

    Great Articles and really what I want. Great job Brian.

    Got a question, have you ever try to place smf player under prism4.0 container. I did try the player is show up but can't play the stream. Here is the code in xaml of view

    <smf:SMFPlayer>
                <smf:SMFPlayer.Playlist>
                    <media:PlaylistItem DeliveryMethod="AdaptiveStreaming" MediaSource="http://video3.smoothhd.com.edgesuite.net/ondemand/Big%20Buck%20Bunny%20Adaptive.ism/Manifest"/>
                </smf:SMFPlayer.Playlist>
            </smf:SMFPlayer>

    Have been trying to find the root cause and can't.

    Thanks


  • George.S

    Re: Working with Prism 4 Part 1: Getting Started


    posted by George.S on Nov 19, 2011 16:48

    Great starter tutorial.  I'd like to see articles including the following topics:

    1. As you suggested, a custom module loader that gets a list of modules a user is authorized to use.

    2. Deeper dive into MEF and Prism -- How to utilize the container in many modules a la Unity;  ImportMany; and clear explanations of how to use other useful techniques beyond the basics.

    3.  Examples of friendly reporting errors to the user from exceptions thrown in various places, especially in a shared Prism service that is utilized by more than one user interface Module.

    Thanks, especially for the clarity of your examples.

    George

     

     

     

  • brian.noyes

    Re: Working with Prism 4 Part 1: Getting Started


    posted by brian.noyes on Nov 20, 2011 00:23

    tle,

    Sorry, but have not tried embedding SMF into a Prism application myself, although I have worked with customers that have and they have not reported any problems with it. There is nothing special about Prism being used to load up the views or the modules. Once they are loaded, they are part of the Silverlight visual tree and the mechanisms of SMF should work fine. I'm afraid I am unable to say what might be causing you problems there.

    Brian

  • brian.noyes

    Re: Working with Prism 4 Part 1: Getting Started


    posted by brian.noyes on Nov 20, 2011 00:24

    George,

    Great suggestions. I'll try to work those into the article on custom module loading to the extent that I can fit it all.

    Brian

  • tle

    Re: Working with Prism 4 Part 1: Getting Started


    posted by tle on Nov 20, 2011 08:37

    Thank Brian, I'll keep post if I get it work.

  • TKelley

    Re: Working with Prism 4 Part 1: Getting Started


    posted by TKelley on Dec 09, 2011 05:34

    Hey Brian,

    Is there a good example of using WCF RIA services with Prism? How would I share services between modules?  Is there a good example of using WCF RIA services with Prism?

    Is there an example of an enterprise application(multiple modules and navigation) that follows the pattern outlined in chapter 9 of your WCF RIA services book?

    thanks,

    Tim





  • brian.noyes

    Re: Working with Prism 4 Part 1: Getting Started


    posted by brian.noyes on Dec 09, 2011 16:10

    Tim,

    I've worked on a number of them, but they are products for companies that don't publicly post their source code. But the patterns discussed in part 9 of my article series / chapter 9 in the ebook shows how to partition the client side libraries and corresponding server side libraries. It would be exactly like that except the client side libraries could be Prism modules if you chose.

  • TKelley

    Re: Working with Prism 4 Part 1: Getting Started


    posted by TKelley on Dec 28, 2011 05:04

    Thanks Brian,

    So for each WCF RIA Services pair, I add the service project reference to the server for hosting and then load the client project via module initialization?

    thanks,

    Tim



  • brian.noyes

    Re: Working with Prism 4 Part 1: Getting Started


    posted by brian.noyes on Dec 28, 2011 15:35

    TKelley,

    There is no reason the client side code has to be in a module or loaded that way. I often will just create a Silverlight class library and reference it from the modules that need those services because often the way you want to factor your client side code into modules does not map 1:1 with the way you want to factor your services. If it is 1:1, then by all means just do the client link to the server library project from the module using the technique I show in the other article series of opening the csproj file and editing the XML link path.

    For the server side, you are correct that you just add references to those libraries from the host. The host will also need the right references and some configuration information in its config file, and the easiest way to get that there is to add a dummy Domain Service to the web host project and then just delete the Domain Service class from there.

  • Ali3dc

    Re: Working with Prism 4 Part 1: Getting Started


    posted by Ali3dc on Dec 30, 2011 13:42

    This Article Is Great , Thank You . You Describe Prism Concepst Very Well And I Agree With Article About how it is possible to download modules depending user identity , Its Gone Be Very Interesting , Thank you Brian

    Ali Azhdari

  • MaherJendoubi2012

    Re: Working with Prism 4 Part 1: Getting Started


    posted by MaherJendoubi2012 on Jun 29, 2012 14:54

    Brian,

    I tried to update the source code of Prism 101 to Prism 4.1 and Silverlight 5. After building the solution, I run the solution and I get the following message: An unhandled exception ('Unhandled Error in Silverlight Application 

    Code: 4004

    Category: ManagedRuntimeError

    Message: Microsoft.Practices.Prism.Modularity.ModuleInitializeException: An exception occured while inilizing module 'CoreModule'.

    Maher Jendoubi.

  • MaherJendoubi2012

    Re: Working with Prism 4 Part 1: Getting Started


    posted by MaherJendoubi2012 on Jun 29, 2012 15:18

    Brian,

    I tried to update the source code of Prism 101 to Prism 4.1 and Silverlight 5. After building the solution, I run the solution and I get the following message: An unhandled exception ('Unhandled Error in Silverlight Application 

    Code: 4004

    Category: ManagedRuntimeError

    Message: Microsoft.Practices.Prism.Modularity.ModuleInitializeException: An exception occured while inilizing module 'CoreModule'.

    Maher Jendoubi.

  • brian.noyes

    Re: Working with Prism 4 Part 1: Getting Started


    posted by brian.noyes on Jun 29, 2012 16:47

    Most likely cause is one of your other references to Silverlight assemblies is on a v4 reference and needs to be updated to a v5 reference. The System.ComponentModel.Composition one is an important one to check since that got updated in v5.

  • MaherJendoubi2012

    Re: Working with Prism 4 Part 1: Getting Started


    posted by MaherJendoubi2012 on Jun 29, 2012 18:39

    I find the answer : Microsoft.Practices.Prism.MefExtensions this assembly should be referenced with Copy Local = True only once (e.g. the main project), and with Copy Local = False in all other projects.

    Thank you Brian! Great article!

    Maher Jendoubi.

  • brian.noyes

    Re: Working with Prism 4 Part 1: Getting Started


    posted by brian.noyes on Jun 29, 2012 18:52

    Ah, yes, that is always required in multi-module Prism projects. Glad you found it, well done.

  • MduduziShelembe

    Re: Working with Prism 4 Part 1: Getting Started


    posted by MduduziShelembe on Jun 29, 2012 19:28
    Great Article! Thanks.
  • Nuitari

    Re: Working with Prism 4 Part 1: Getting Started


    posted by Nuitari on Jul 19, 2012 12:48
    Thank you for the article, very helpful for getting started with Prism! The Template Pack worked with Silverlight 5/Prism 4.1 without problems.
  • VuyiswaMaseko

    Re: Working with Prism 4 Part 1: Getting Started


    posted by VuyiswaMaseko on Sep 24, 2012 22:49
    It seems like the site that has PRISM Templates has been abondoned http://dphill.members.winisp.net/Templates.html
    
    
    
    
    
    
    
                            
  • ethan

    Re: Working with Prism 4 Part 1: Getting Started


    posted by ethan on Dec 27, 2012 22:55

    Will the Application.Current.CheckAndDownloadUpdateAsync() cause modules that have been modified to be updated?

  • brian.noyes

    Re: Working with Prism 4 Part 1: Getting Started


    posted by brian.noyes on Dec 28, 2012 01:04

    It should, as long as you have bumped the version info and deployed the updated XAP to the web site that the originals came from.

  • ethan

    Re: Working with Prism 4 Part 1: Getting Started


    posted by ethan on Jan 04, 2013 00:45

    Since the ModuleCatalog.xaml file is a resource, wouldn't the DLL have to be recompiled if the catalog was modified? If so, I take it the statement "outside the compiled code so that it could potentially be changed on the deployment site to add or remove modules without needing to recompile anything" is referring to a ModuleCatalog.xaml file that would be hosted on the web site?

    Thanks again for this great series of articles. I feel like I'm one or two hurdles away from having everything just like I need it.

  • brian.noyes

    Re: Working with Prism 4 Part 1: Getting Started


    posted by brian.noyes on Jan 04, 2013 01:09

    Hi Ethan,

    I hate it when my reading audience actually pays attention to everything I say. :)

    Guilty as charged. When I wrote that, I had in mind the experience of WPF and putting your module configuration in a .config file that you could actually change without recompiling. You are correct that due to the deployment model of Silverlight, you would have to recompile. 

    However, the modularity in Prism is extensible, it is not too hard to write your own IModuleCatalog, register it with the container instead of the default one, and have yours make a web service call to pull down the configuration (possibly different on a per user basis in that case if they are authenticated and you know their identity on the client side with something like RIA Services security - see my other article series on that) at runtime from the server side. That configuration could then take on any shape you like and be modified as often as needed without any redeployment being necessary.

    Here is one such example: http://dotnet-mcts.jecaestevez.com/2011/05/using-server-side-prism-module-catalog.html


    hope that helps.

    Brian

  • michael88

    Re: Working with Prism 4 Part 1: Getting Started


    posted by michael88 on Feb 14, 2013 13:20

    Hi, 

    I am a beginner in prism, was trying out your sample.

    as you said in step 10 , I have added a region in shell .

    If I have multiple modules, how do i add multiple views to the same region ?

    As of now am not able to see the second view in the region.

    Thanks,..




  • brian.noyes

    Re: Working with Prism 4 Part 1: Getting Started


    posted by brian.noyes on Feb 14, 2013 15:19

    If your container control is a ContentControl, then you will have to call Activate on IRegionManager in each module to show each one at a time. If you put them in a TabControl, they should all show up as tabs, and Activate just selects the tab. Each module just needs to Import (if using MEF) or declare a constructor dependency (if using Unity) to inject IRegionManager so that they can call Add with the appropriate region name to add their views to it.

  • michael88

    Re: Working with Prism 4 Part 1: Getting Started


    posted by michael88 on Feb 18, 2013 15:05

    Thanks Brian, 
    I used tabcontrol and it worked perfectly. So each view activates on tab respective selection. How do I put a tab header? It should be different for different tab/view ?

    I really appreciate the effort you have put in. Really usefull for a begineer like me :)

    cheers !!


  • brian.noyes

    Re: Working with Prism 4 Part 1: Getting Started


    posted by brian.noyes on Feb 19, 2013 00:06

    Prism will make sure that your ViewModel is the DataContext at the TabItem level. That means you can set up a style to set the Header of the TabItem based off a property exposed from your ViewModel:

     <Style TargetType="TabItem">
                <Setter Property="Header" Value="{Binding Content.DataContext.HeaderInfo, RelativeSource={RelativeSource Self}}"/>
            </Style>

  • jlsfernandez

    Re: Working with Prism 4 Part 1: Getting Started


    posted by jlsfernandez on Jul 22, 2013 09:16

    Hello Bryan

    First Thanks a lot for your series (and for your PluralSight Courses also ;-) )

    I would like to know (i toke your reflexion about a future good article) how to extend the module catalog to have a per user functionality availability modules list.

    Also it would be good if you could let me know where i can find and example that let me have a  previous to the Silverlight application an HTML5 login page with WIF (Oauth capabilities) that later the silverlight application can use.


    Thanks a lot

  • brian.noyes

    Re: Working with Prism 4 Part 1: Getting Started


    posted by brian.noyes on Jul 22, 2013 17:25

    Hi jlsfernandez,

    Thanks for the feedback. I could certainly entertain doing a follow on post about a custom user authorization-based module catalog. I'm been focusing more on WinRT and HTML 5 topics lately, but will try to find some time to write an article on that.

    For the authentication question, what you are describing is certainly possible if the Silverlight app is running in the browser, but the details of how to go about it are complicated and I don't know them off the top of my head. A better source for OAuth articles and blogging is Dominick Baier (www.leastprivilege.com).

  • JMDEV

    Re: Working with Prism 4 Part 1: Getting Started


    posted by JMDEV on Aug 19, 2013 00:16

    Hello Bryan

    First Thanks a lot for your series ,very helpful for getting started and understand with Prism!

    with visual studio 2013 and Windows 8  ,I need to Had  "InitializationMode="WhenAvailable" , in ModuleCatalog.xaml , if not I can't see the messagebox (Firefox or IE).


  • brian.noyes

    Re: Working with Prism 4 Part 1: Getting Started


    posted by brian.noyes on Aug 19, 2013 15:38

    Hi JMDEV,

    Thanks for the info. Had not tried it yet on VS 2013 and Win 8. I'll have to look into that, but I suspect it is because they are doing more and more asynchronous stuff under the covers, so the code path that assumes everything can be loaded synchronously may not be working the same any more.

    I'll let the Prism team know.

  • brian.noyes

    Re: Working with Prism 4 Part 1: Getting Started


    posted by brian.noyes on Aug 19, 2013 15:41

    Actually, I didn't think that response through. Nothing has changed at all in terms of Silverlight in 2013 release or on Win8 (other than only being able to run Silverlight from the desktop IE, not the start screen IE). So I can't explain why that would be the case.

  • JMDEV

    Re: Working with Prism 4 Part 1: Getting Started


    posted by JMDEV on Aug 27, 2013 00:37
    HI , the tuto  doesn't work on my config (win8 and vss2013 preview ) because the "Build Action" of  the "ModuleCatalog.xaml " wasn't on "Page" and in my first comment if  I saw the messagebox its because I put it on the mainpage after try some solutions , sorry. and thanks a lot  for this series
  • jan2013

    Re: Working with Prism 4 Part 1: Getting Started


    posted by jan2013 on Sep 10, 2013 11:55

    Hi Brian,

    I just wanted to say a big thank you for this great article!! I've been searching the internet for introductions to PRSIM, and even though I'm only reading through part one I can already tell this is by far the best explanation I've found as it has helped me understand a few fundamental topics in just a few lines of text - well done!!


    Cheers,

    Jan

  • brian.noyes

    Re: Working with Prism 4 Part 1: Getting Started


    posted by brian.noyes on Sep 10, 2013 13:31

    Hi Jan,

    Glad you are enjoying it. For more in depth coverage, definitely check out the book I co-authored with the team, available online here: http://msdn.microsoft.com/en-us/library/gg406140.aspx, also downloadable as PDF or purchasable as a print book from O'Reilly.

    Thanks

    Brian

Add Comment

Login to comment:
  *      *       

From this series