Commanding in Prism


In the screenshot below, the button is enabled because the text box does not contain ‘NO’:


In the screenshot below, the button is disabled because the text box does contain ‘NO’:


The comments in the code screenshots probably cover everything:




using System.Windows;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using System.Windows.Input;

namespace CommandImpl.ViewModel {

/// A basic test of the Prism implementations of Commanding, using DelegateCommand and NotificationObject.
/// Next steps are how to test this.

public class CommandViewModel : NotificationObject {

private ICommand _submitCommand;
public ICommand SubmitCommand {
get { return _submitCommand; }
set { if (_submitCommand == value) return;
_submitCommand = value;
RaisePropertyChanged(() => SubmitCommand);

public CommandViewModel() {
SubmitCommand = new DelegateCommand(Submit, CanSubmit);

public bool CanSubmit(string cmdParameter) {
if (cmdParameter == null) return true;
return !(cmdParameter.Contains(“NO”));

private static void Submit(string cmdParameter) {
// Although I only want the command parameter for the CanSubmit, it appears there is no ctor
// that takes no parameter for the Execute, but a parameter for the CanExecute
// The idea is possibly I guess that you pass-through the value to the CanExecute method.
// And the plus anyway is that if CanSubmit returns false, you never reach here anyway.
// But given that, it does mean that cmdParameter is always redundant, as you clearly do not invoke
// CanSubmit from here.
var msg = string.Format(“Value in textbox: [{0}]”, cmdParameter);
//msg = CanSubmit(cmdParameter) ? “Can execute” : “Cannot execute”;


Prism: MEF in a Silverlight context

This post is about using MEF in Silverlight, and specifically getting going using the quick starts in the Prism (4.1 in my case) download.

I’ve mentioned the Prism Developer’s Guide. For MEF and Silverlight it points you to the .bat highlighted below, which just starts up that solution.


I’m using Visual Studio 2012, whereas the Prism 4.1 Quickstarts assume VS2010. Hence I get a bunch of upgrade alerts:




Nothing to worry about, as it all compiles and runs fine:

QuickStartUpgrade04 QuickStartUpgrade03




Browsing through the code, we see that, in places anyway, the Quickstart has implemented its own INPC, rather than just using the Prism version:



If I remove the System.ComponentModel using, to force the issue, then we can see where we need to update things:



We replace INPC with NotificationObject from the Prism Libraries:


This then prompts us with the new Using:


I made the mistake on redoing this of looking for a specific Prism ViewModel DLL on the file system. Doh. You just want the general, Silverlight specific, Prism DLL:


With that in place, we can now just remove the helper event handler and method.

Also remove the string constants, as we’ll be replacing these with lambdas:QuickStartUpgrade11


Now we have broken stuff…



… until we make the change to a lamdba (a minor performance overhead, as everyone notes):


, and then generally tidy up:


Finally make sure it still all runs (you’ll just have to take my word this is a fresh screenshot):


quod erat demonstrandum, as we used to say, when I was a boy.




The Prism Developer’s Guide

Ever seen this?


The manual is good and it’s free, and the full download comes with a ton of worked examples. This is the Prism 4 Guide, and this is the Prism 4.1 download, including the examples.

In fact here’s one I hadn’t noticed – a manual about Prism and Windows 8.1 – surely worth a browse.



And just searching for DelegateCommand references, just as a for instance…


Slightly tangentially, you can replace the RelayCommand references with Prism DelegateCommands, and everything continues to work:








The top row of Order 11088 is one I created using the Add button, edited to change the Freight value, and then used the Save button.

To prove it made its way to the database:


May 2015…

And now we have this


, and this


The date suggests I just missed it last time round. This looks good:


And I should back this up somewhere in case it disappears:


Argh… looks to be the same. But for (my) reference I’ve saved it to my Adobe cloud.

And a final link to the QuickStarts etc.

Prism, DI, IOC


The Prism manual, in talking about DI and IOCs, references this article from MSDN Magazine.

Frustratingly, both in Chrome and IE (you would have felt Microsoft could have tried harder in eating their dog food), code listings appear like this:


Honestly, what are you supposed to make of that?

Well, once you run it through the code beautifier I’ve reference elsewhere, you can get this:

public class InvoiceService {
	private readonly AuthorizationService authoriazationService;
	private readonly InvoiceValidator invoiceValidator;
	private readonly InvoiceRepository invoiceRepository;
	public InvoiceService() {
		authoriazationService = new AuthorizationService();
		invoiceValidator = new InvoiceValidator();
		invoiceRepository = new InvoiceRepository();
	public ValidationResults Submit(Invoice invoice) {
		ValidationResults results;
		CheckPermissions(invoice, InvoiceAction.Submit);
		results = ValidateInvoice(invoice);
		return results;
	private void CheckPermissions(Invoice invoice, InvoiceAction action) {
		if (authoriazationService.IsActionAllowed(invoice, action) == false) {
			throw new SecurityException("Insufficient permissions to submit this invoice");
	private ValidationResults ValidateInvoice(Invoice invoice) {
		return invoiceValidator.Validate(invoice);
	private void SaveInvoice(Invoice invoice) {

Much better.


But Serendipity led me to this um aggregator site about fonts, and then this article specifically on handwriting-type fonts (yup). One to digest later: at its tidiest, I’d be happy to see my handwriting rendered as a font.

Later… so that Sigmund Freud font costs a bit – fair enough obviously. This site though has free fonts, including handwritten ones. Sure, you get what you pay for.Well, as we know, in Software, that is not always true, as there is a lot of great free stuff.