Prism 2.0 download bits refreshed. What’s changed?
February 27th, 2009
A week ago we announced the Composite Application Guidance for WPF and Silverlight - February 2009 release.
A few days after, Microsoft released a new version of Silverlight (version 2.0.40115.00 or just GDR1), and it broke a build script that we were using for some of the unit tests in the CAL. This didn’t prevent the users from using the CAL, but was causing build failures with cryptical error messages. You can read the full details here.
We also got feedback from users (i.e here, here and from Brian Noyes), that when migrating an existing Prism 1.0 app to Prism 2.0 (WPF), they were experimenting inconsistencies with the activation and deactivation of views when using a TabControl as a region. You can read the full details here.
We decided to fix both issues and refresh the download bits, so you can now download them from MSDN.
What files were updated in the source code?
If you are including the CAL in you application’s source control, you might want to know exactly what files were updated.
The following are the only files updated with this refresh:
- CAL/Silverlight/Composite.Tests/Mocks/Modules/createXap.bat
- CAL/Silverlight/Composite.Tests/Modularity/XapModuleTypeLoaderFixture.cs
- CAL/Desktop/Composite.Presentation/Regions/Behaviors/SelectorItemsSourceSyncBehavior.cs
- Desktop/Composite.Presentation.Tests/Regions/Behaviors/SelectorItemsSourceSyncRegionBehaviorFixture.cs
Do you have the latest version already?
If you are not sure whether you already have the latest version of the self-extractable EXE for the source code (because the version number was not updated) you can check the timestamp of the Digital Signature. The refreshed bits should read ‘Tuesday, February 24, 2009′ (don’t mind the time, as it varies with the time zone of the PC).
More info on the refresh on Blaine Wastell’s blog and Bob Brumfield’s blog.
Happy composite application building!
Prism 2.0 released: Composite Application Guidance for WPF and Silverlight – February 2009
February 16th, 2009
Great news guys: the Composite Application Guidance for WPF and Silverlight - February 2009 has been finally released.
You’ll be glad to know that the final version of this new release is out. If you’ve been playing with the bi-weekly drops, or holding off to be able to use this already embraced guidance from WPF, but in your Silverlight projects, you can now start grinning.
Intended Audience
This guidance is intended for software architects and software developers building enterprise Windows Presentation Foundation (WPF) or Silverlight applications. Such applications typically feature multiple screens, rich, flexible user interaction and data visualization, and role-determined behavior. Additionally, these applications typically use layered architectures that may be physically deployed across tiers, strong separation of concerns, and loosely coupled components. They are “built to last” and “built for change.” This means that the application’s expected lifetime is measured in years and that it will evolve in response to new, unforeseen requirements. Applications that do not demand these features and characteristics may not benefit from the Composite Application Library.
The Composite Application Guidance is not difficult to learn, but developers must be ready and willing to embrace patterns and practices that may be new to them.
Composite Application Guidance Assets
- Composite Application Library source code. Developers can use the Composite Application Library to develop multi-targeted applications in WPF and Silverlight that are composed of independent and collaborating modules.
- Unity Extensions for Composite Application Library source code. This provides components to use the Unity Application Block with the Composite Application Library.
- Stock Trader Reference Implementation (Stock Trader RI). This is a composite application that is based on a real-world scenario. This intentionally incomplete application illustrates the composite application baseline architecture. This is a good reference to see how many of the challenges when building composite applications are addressed by this guidance.
- QuickStarts. This includes the source code for several small, focused applications that illustrate a specific challenge.
- Documentation. This includes the architectural overview, Stock Trader RI overview, design and technical concepts for composite applications, applied patterns, How-to topics, QuickStarts overviews, and deployment topics. Much of this guidance is applicable even if you are not using the Composite Application Library, but you just want to know best practices for creating composite applications.
![]()
What’s new from Prism 1.0
- Silverlight Support. The complete Composite Application Library, RI, QuickStarts, documentation (everything!) has been brought into life in Silverlight. The source code is targeting both WPF and Silverlight, by using linked source code files (there is of course guidance on how to develop applications that reuse code for these 2 platforms if you are planning to target both of these with the same code).
- WPF developers won’t see much difference in the Composite Application Library itself, but there are some reasons to update:
- View Discovery UI Composition. Modules can register views (or presentation models) against a named region. When that region is displayed at run time, any views that have been registered for that location will be automatically created and displayed within it. The Prism 1.0 way of adding views to a region is now officially called View Injection, because it does not work as a configuration mechanism, but as a explicit demand to add the view into a specific region instance.
- RegionContext is useful when you want to share context between a parent view and child views that are hosted in a region. Think of this like flowing DataContext down, but through regions.
- ICommand attached behavior base. Base class to help create behaviors for control events that will fire an ICommand. This was mostly done to support executing commands in Silverlight when clicking a button, but could be applied equally well to WPF for other kind of events (such as pressing the Enter Key on a search box as it’s being demonstrated in the RI). The basic idea around behaviors for ICommand is similar to what I blogged about in this post a while ago.
- Region extensibility. Regions are easier to extend with their functionality moving into attached region behaviors.
- Module Catalog. This concept is easier to understand and use than the Module Enumerator and Module Loader present in 1.0. Nevertheless, migrating to the catalog from enumerators is pretty straightforward and it’s explained in the docs.
- Project Linker. Basic tooling that helps you keep 2 projects synchronized by creating Visual Studio file links from one project to another. This is useful for sharing source code that targets both WPF and Silverlight platforms (as there is no binary compatibility).
What is different between the Silverlight and the WPF version of the Composite Application Library in this release?
- The codebase is mostly the same, except for some minor differences. The biggest of these is that Directory Lookup of modules and Configuration based module catalog is available in WPF only, whilst Silverlight supports retrieving modules asynchronously in separate XAP files, to reduce the main application size (and startup time).
Resources
- MSDN Landing page: www.microsoft.com/compositewpf
- Community site: www.codeplex.com/prism
- Download: Composite Application Guidance for WPF and Silverlight
Also check out this introductory video to Prism from Blaine Wastell (Prism team’s Program Manager).
Prism V2’s drop 7 is out: Updates to Commanding and Modularity
December 7th, 2008
A new drop of Composite Application Guidance for WPF and Silverlight (or Prism) is out. Get it from Codeplex.
The latest changes since drop 6 include:
- Modularity
Many changes where made to Modularity to keep it simpler (although not extremely noticeable for the surface API consumer).
We also re-included the Directory Lookup and Configuration based module enumerators for WPF (the ones that were included in Prism V1), and the respective QuickStarts that show their usages.
- Commanding
We included the Commands namespace in Silverlight’s CAL.
In order to hook up an ICommand to a button from XAML, we are following a similar approach to what we’ve done in a previous spike and blogged about it here, so I won’t go into many details about it in this post.
The CommandParameter cannot be set from XAML currently; that feature did not make it in drop 7 for a few minutes, but this will definitely be possible in the next drop.
We also ported the Commanding QS to Silverlight, although this is still work in progress.
- Regions
There is a breaking change in the Regions property of the IRegionManager, which may not be noticeable for the majority of users, but it can break some of your RegionManager mocks if you are doing TDD (and if you are not… WHY NOT?!?!?
)
- Documentation
Changes in the documentation includes explanation of the new Pull-based Composition QuickStart and the Modularity QuickStarts for WPF.
- Project Linker
If you like ProjectLinker or you would like to try it, but didn’t feel like installing Visual Studio 2008 SDK, you will be happy to know that it does not require the SDK anymore
![]()
There is a separate download for the ProjectLinker installer and for its source code, as the latter still requires the SDK if you want to update it and build it yourself.
I hope you like it, and as usual, we are driven by the community, so your feedback is very useful to us.
Prism V2: The 6th drop is out (a.k.a. Composite Application Guidance for WPF and Silverlight)
November 24th, 2008
The 6th drop for this project is already out of the team room and into Codeplex, as it has been every 2 weeks since 2*(6-1)=10 weeks back from today.
You can see the complete list of latest changes in Erwin’s post.
The biggest changes for this drop, although not very noticeable from the outside if you’re just using the default way that Regions work without the need to extend them, are:
- Added RegionContext
This is to provide a way to pass some context to the Views that are placed inside a particular instance of a region. You were usually able to workaround the need for this context by using EventAggregator or scoped DI containers, but luckily this will be easier to achieve for those same scenarios and for more complex ones (for example when using scoped region managers and having several instances of the same region).
- Formalized infrastructure for extending the behavior of Regions
In Prism 1.0 there were already some ad-hoc behavior classes for extending the functionality of the regions (for example the CollectionActiveAwareBehavior or SelectorRegionSyncBehavior).
In Prism V2, we also had to rethink how the RegionManager attached properties would work in the Silverlight world, as it is different from WPF (Silverlight lacks property value inheritance). We also added features like the new Pull-based composition scenario, or the RegionContext attached property.
We started adding separate behavioral objects for these new features, which were very flexible, but very hard to understand, debug, and to know which objects were currently modifying the behavior of a particular region instance.
This created the need for an infrastructure for all of these formerly independent behavior objects, that would make the library easier to understand, extend and maintain.
The IRegion interface now has a collection of IRegionBehavior objects, so you can always know which objects are extending the functionality of a particular Region instance, and it can also couple the lifetime for these behaviors and regions, in order to prevent memory leaks when developing new behaviors.
Picture taken from Erwin’s blog post. Read the full post hereICommand for Silverlight 2.0 RTW Updated Sample
November 21st, 2008
Some time ago, we blogged about how to implement the ICommand feature that is available in WPF but missing in Silverlight.
The sample included in that post was using Silverlight 2 beta 2, so I updated the code now to target Silverlight 2 RTW. The main difference is that the old code included the ICommand interface which is now available directly in the Silverlight 2 bits (although there is no class implementing it, nor the input controls know how to bind to it).
Read the full blog post here:
Downlad the updated sample code here:
Composite Application Guidance (Prism) vNext envisioning
September 4th, 2008
If you want to know what has been keeping us busy for Prism vNext, read this detailed post from David Hill.
An excerpt from his post:
Prism 2.0 will focus on two things:
- Extending the guidance delivered in Prism 1.0 for building composite WPF applications to also support composite Silverlight applications.
- Adding guidance for building ‘multi-headed’ applications - applications that can deliver both a desktop and an in-browser experience.
David Hill has been working and contributing with p&p in many projects, and has recently become the new architect at p&p. So be sure to add David to your RSS, because there’s probably lots more to come related to the guidance that p&p is providing not only with Prism vNext, but with other assets like EntLib.
ICommand for Silverlight with Attached Behaviors
August 6th, 2008
Silverlight has many things in common with WPF, but also many different or missing things because it is a “subset” of it.
One of the missing things is support for the ICommand interface (UPDATE: in Silverlight 2, this interface was included, but there is no implementation that uses it, so this post is still useful and the sample was updated to use Silverlight’s interface instead of my own copy of it from WPF). I find this interface (and the button’s behavior when bound to a command) extremely useful/necessary, especially when using DataTemplates bound to a Presentation Model (to provide the view for the model), as hooking events (like Click) is not an option in this scenario.
<Button Content=”Save” Command=”{Binding SaveOrderCommand}” />
As you might suspect, we (at p&p) are now spiking and seeing how the Composite Application Library for WPF (a.k.a. Prism or CompositeWPF) would work on Silverlight. One important part of the guidance provided is to use Commands for communication between the moving parts, and from the view (this is specially useful for testability).
Today the team published a small drop that contains some of the spikes we’ve been playing with, including an initial quick & dirt port of the library. We added the ICommand interface that is missing from WPF to the same System.Windows.Input namespace, not only to avoid adding new using statements all throughout the solutions, but in my opinion, because we hope Silverlight might support this out-of-the-box in the future, and we might be able to quickly switch to the provided implementation easily if they stay consistent with WPF’s API. (UPDATE: the interface is now part of Silverlight 2)
namespace System.Windows.Input { public interface ICommand { bool CanExecute(object parameter); void Execute(object parameter); event EventHandler CanExecuteChanged; } }
Once the ICommand interface is there, the DelegateCommand and CompositeCommand implementations included in CompositeWPF start working immediately and I can now call command.Execute(null) on my commands… big deal! But I want to hook my buttons to those commands from XAML!
public class DelegateCommand<T> : ICommand { … }
Well, how to hook to a command using XAML? easy: using attached behaviors. First, I need to define an attached property that will contain the command.
public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(“Command”, typeof(ICommand), typeof(Commands), new PropertyMetadata(CommandPropertyChanged));
Ok, I can attach the command to a button, but how will it be invoked when the button is clicked? Well, that’s when CommandPropertyChanged kicks in (and that’s why it’s an attached behavior, and not just a simple attached property):
private static void CommandPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { ButtonBase element = o as ButtonBase; if (element != null) { if (e.OldValue != null) { CommandButtonBehavior behavior = (CommandButtonBehavior)element.GetValue(CommandButtonBehaviorProperty); behavior.Detach(); element.SetValue(CommandButtonBehaviorProperty, behavior); } if (e.NewValue != null) { CommandButtonBehavior behavior = new CommandButtonBehavior(element, (ICommand)e.NewValue)); behavior.Attach(); element.ClearValue(CommandButtonBehaviorProperty); } } }
The code above creates an instance of a behavior class that contains the behavior. This instance is stored in a private attached property, in order to be able to retrieve the behavior and Detach it when unhooking the command (when e.OldValue is not null).
The following code shows how CommandButtonBehavior hooks to the Click event of the Button in order to invoke the command.
private class CommandButtonBehavior { private readonly WeakReference elementReference; private readonly ICommand command; public CommandButtonBehavior(ButtonBase element, ICommand command) { this.elementReference = new WeakReference(element); this.command = command; } public void Attach() { ButtonBase element = GetElement(); if (element != null) { element.Click += element_Clicked; } } public void Detach() { ButtonBase element = GetElement(); if (element != null) { element.Click -= element_Clicked; } } private static void element_Clicked(object sender, EventArgs e) { DependencyObject element = (DependencyObject)sender; ICommand command = (ICommand)element.GetValue(CommandProperty); object commandParameter = element.GetValue(CommandParameterProperty); command.Execute(commandParameter); } private ButtonBase GetElement() { return elementReference.Target as ButtonBase; } }
The reason for storing the button as a WeakReference, is to avoid the application from leaking memory when trying to destroy a view with a button that has not correctly unhooked the command.
Ok, I believe that this bunch of code might be helpful, but how to use it from XAML? Again, easy:
<Button Content=”Save” Commands:Commands.Command=”{Binding Path=SaveOrderCommand}” />
As you can see, this looks way too similar to how WPF commands are declared, except for the Commands:Commands part of it, because this is an attached property and not a direct dependency property of the Button class.
I’m attaching a sample that uses the classes published in Codeplex by the Prism team. The sample includes support for CommandParameter and allows enabling and disabling the button depending on whether the ICommand.CanExecute method returns true or false.
Note: Even if I’m using DelegateCommand, you could use your own implementation of ICommand, as the attached behavior is not coupled to any implementation of ICommand.
Disclaimer
This code is provided “AS IS” with no warranties, and confers no rights.
Download
You can get the sample with the attached behaviors from here (updated for SL 2 RTW), or you can download the code for the behaviors without the sample from the latest change set of the CompositeWPF source control, in the spikes folder.
More information
Nikhil Kothari has a great introduction and samples for attached behaviors on Silverlight here.
John Gossman blogs about attached behaviors on WPF (here and here).
Dave Relyea created a neat example of attached behavior on Silverlight here.
Ezequiel Jadib has more info on the spike published in Codeplex here.