Composite Application Guidance for WPF & SL (Prism-v2) C# Snippets with Guidance, Links and Tips
September 7th, 2009
Most developers are familiar with code snippets. They are really useful while developing code, mainly because they make some of the most common developing tasks easier and faster, by making us just fill in a couple of blanks.
This is great, but Julian had an idea that I believe will make the code snippets we created even more useful. He said: “What if we not only provide the code snippets, but we also add some guidance, links and tips based on our experience as Prism developers, that will help others with these common tasks”. Matias, Ezequiel and me thought this was a great idea, specially for people starting with Prism. The guidance offered in the snippets will provide insight about how Prism works, leading to better and faster development. This guidance can simply be deleted once the code is put into place as it is just composed of code comments.
Without further introduction, the code snippets we created are useful for the most common ways of performing the following tasks (between brackets how to insert the snippet in code):
- View Discovery (prismviewdisc)
- View Injection (prismviewinj)
- Event Publishing (prismeventpub)
- Event Subscribing (prismeventsub)
- Command with attached behavior (prismcmdbehavior, this was updated from this snippet)
Below you can find a picture of the generated code for the Event Subscription snippet, so you can get an idea of about these snippets with tips:
Pre-Requirements
- Download the Snippet Visual Studio installer and run the installer (the snippets are provided “AS IS” with no warranties and confers no rights). You can also get the .zip file from here if you want to edit the snippets.
I hope the snippets help you learn Prism while developing. As always your feedback is really appreciated.
Composite Application Guidance (Prism-v2) sample application using Silverlight 3 Child Window
June 30th, 2009
Today I tweaked the Event Aggregator Quickstart that comes with the Prism-v2 source code to show how Silverlight 3 Child Windows could be used in a Silverlight Composite Application.
Child Window Overview
Silverlight 3 has a new template called ChildWindow. It is basically a user control (unlike a Popup you can edit its XAML), but it allows you to create modal windows for your application, such as dialogs.
To use this dialog, you just have to create a new instance of it and call its Show method. As this call is asynchronic, you need to handle the Closed event to know when the dialog interaction has finished.
Sample Scenario
The Quickstart’s scenario shows how to use the Event Aggregator to add funds of a particular customer, so what I did is add a confirmation dialog before adding the fund. After you select the customer and fund (both of them as in the original Quickstart) and click the Add button following dialog pops up:
As expected, if you click “Yes” the fund will be added, and if you click “No” or close the dialog it won’t.
Child Window Sample
Disclaimer
This code is provided “AS IS” with no warranties, and confers no rights.
Download
You can get the sample from here: ChildWindowSample.zip.
WebBrowser control Quickstart for the Composite Application Guidance for WPF and Silverlight (Prism-v2)
June 23rd, 2009
Last week, with Matias Bonaventura, Julian Dominguez and Pablo Constantini, we created a WPF demo application using Prism-v2 that shows both way interaction between different modules of the application and different WPF WebBrowser controls.
Below you can find some of the highlights of how this application works. The demo shows interaction between the WPF application and a Web application hosted in a WebBrowser control. How this is done is explained with further detail in the blog post.
Quickstart Overview
The demo has the following main components:
- CustomersModule: This module is responsible for handling the information of the different customers. It gets the information from a WCF WebService. It has a single view, CustomerView, with a DataGrid to show the customers. The view is placed in the “CustomersRegion” (top left of the screen).
- LocationModule: This module contains a single view which in turn hosts a WPF WebBrowser control (and a progress bar to show when the page is loading). This control shows the place of residence of the customer through Bing Maps. This view is placed in the “LocationRegion” (bottom of the screen).
- OrdersModule: This module contains a single view which has a WPF WebBrowser control. This control shows a different ASP.NET page with a customer order, based on the customer that is selected in the CustomersView DataGrid. The view is shown in the “OrdersRegion”.
- A Web Application that allows shipping different customer orders.
- A WCF service that manages Customers and Orders (this is for the Web app and the WPF app to share the same data).
The image below shows the layout of the application:

How does the application work?
When a customer from the DataGrid is selected a command is executed (through an attached behavior with the DataGrid’s SelectionChanged event). This command publishes an event using the EventAggregator to notify the Presenter’s of the OrdersView and LocationView in the other modules.
The OrdersView updates the page it shows in the WebBrowser control, and the LocationView updates the customer’s address in a BingMap.
When the user clicks the “Ship Order” button from the WebSite hosted in the WebBroser control from the OrdersView:
- The WCF Web Service is updated with the data of the Shipped Order (marks the order as shipped).
- The view listens to the published JS event in the site hosted by the WebBrowser control and notifies its Presenter (I go over how this communication takes place later in this post).
- The Presenter fires an event through EventAggregator.
- The event is handled by the CustomersViewModel which updates the customer list through the WebService.
- The Customers DataGrid is updated.
Implementation Specifics
To enable the WebSite to communicate with the WebBrowser control (via JS events) the following steps were performed:
- Created the OrdersScriptableModel class (located in the OrdersView code behind), which is serialized as JS.
- Fire a JS event from this class in the page and this in turn notifies the view.
- Publish an event (using the EventAggregator) to establish communication with the CustomersViewModel to update the customers list (performing a call to the WebService).
There are other possible approaches, such as going over the different controls in the current WebBrowser.Document to listen for JS events.
You can also check out some tips about using WCF services in Prism in this post by Matias.
Source Code
Disclaimer
This code is provided “AS IS” with no warranties, and confers no rights.
Download
We uploaded the sample to the Prism codeplex site. You can get the sample from here.
Hope this sample application is useful!!!
This is a post I have had pending for a long time. As usual it came up after I saw this question in the Composite WPF & Silverlight forums at Codeplex.
The user wanted a lightweight download, and was only using the Event Aggregator from the CAL’s latest bits. I thought that this would really test Prism’s extensibility, so I created a single assembly which only contained things required by the Silverlight Event Aggregator.
| UPDATE: This assembly also works when using Silverlight 3 Beta version. |
The following picture will easen the process of explaining/understanding all the classes this assembly requires. It was taken using the .Net Reflector. (Clicking it will probably make it easier to read)
Afterwards I tested the correct functionality of this assembly using the EventAggregator Quickstart by replacing the events used in it with events from this new assembly.Everything worked like a charm, so Prism-v2 passed this “extensibility test”
Glenn Block has a post that explains how to perform Event Aggregation with MEF (with and without EventAggregator), so perhaps you could find that post interesting if you have read this far.
You can check out other posts in this series:
Standalone Silverlight Event Aggregator Binaries
Disclaimer
This assembly is provided “AS IS” with no warranties, and confers no rights.
Download
You can get the assembly here: Composite.EventAggregator.Silverlight.



