Commanding in Prism

commanding01

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

commanding02

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

commanding03

The comments in the code screenshots probably cover everything:

commanding04

commanding05

commanding06


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.
/// http://msdn.microsoft.com/en-us/library/gg431410(v=pandp.50).aspx
// http://msdn.microsoft.com/en-us/library/system.windows.input.icommand(v=vs.110).aspx
// http://jeremybytes.blogspot.co.uk/2012/12/five-months-in-working-with-prism-in-wpf.html
// http://msdn.microsoft.com/en-au/library/ff921102.aspx
// http://msdn.microsoft.com/en-us/library/gg419054(v=pandp.50).aspx
/// 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);
MessageBox.Show(msg);
//msg = CanSubmit(cmdParameter) ? “Can execute” : “Cannot execute”;
//MessageBox.Show(msg);
}
}
}

Advertisements

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.

QuickStartUpgrade05

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

QuickStartUpgrade02

QuickStartUpgrade01

 

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

QuickStartUpgrade04 QuickStartUpgrade03

 

 

ModuleTrackingState.cs

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

QuickStartUpgrade06

 

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

QuickStartUpgrade07

QuickStartUpgrade08

We replace INPC with NotificationObject from the Prism Libraries:

QuickStartUpgrade09

This then prompts us with the new Using:

QuickStartUpgrade10

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:

prism01

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…

QuickStartUpgrade12

 

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

QuickStartUpgrade13

, and then generally tidy up:

QuickStartUpgrade14

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

QuickStartUpgrade15

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

 

 

 

The Prism Developer’s Guide

Ever seen this?

PrismManual01

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.

PrismManual02

PrismManual03

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

PrismManual04

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

ICommand05

ICommand06

ICommand01

ICommand02

ICommand07

ICommand03

ICommand04

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:

ICommand08

May 2015…

And now we have this

PrismLatest01

, and this

PrismLatest02

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

PrismLatest03

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

PrismLatest04

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

Prism

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:

BadFormatting01

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);
		SaveInvoice(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) {
		invoiceRepository.Save(invoice);
	}
}

Much better.

Fonts

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.