A place for spare thoughts

27/09/2011

WPF 4.5 supports MarkupExtension in the event properties

Filed under: wpf — Ivan Danilov @ 08:22

At last! Since the beginning of MVVM pattern it was some kind of a miss in the framework: when control supports commands you have more or less convenient model of work – create command in the ViewModel and bind control’s action to it. But if you want to use some event as a trigger for command – you can say goodbye to simplicity and conciseness of your code.

The cleanest approach I am aware of is to declare some attached behavior that will act as a bridge between control’s events (as it will subscribe and unsubscribe when needed) and ViewModel’s commands (receive command instance from binding and invoke it when event is raised). This approach has one major downside – you need to write new attached behavior for each control/event pair you need to use. It is easy, boring and error-prone.

For RoutedEvents there’s better option as we have some means to pinpoint event’s tail directly in XAML with compile-time checking. I won’t stop on it in details as you could read about it here and here or here.

There’s of course ‘heavy artillery’ variant in form of reflection (make attached behavior and specify event name as string), but it doesn’t have compile-time checking, IntelliSense and many other useful things so I don’t consider it a viable approach at all in day-to-day work.

With WPF 4.5 things are finally became better. Now you can write MarkupExtensions for events thus the main problem of the first approach (namely: impossibility to specify event in the clean manner for attached behavior) is defeated. Jonathan Antoine already wrote an example how to use this new feature here and here. He is showing only general technique, but replacing method call with command invocation seems almost trivial. What I’d like to have is something as below code:

<Grid PreviewMouseDown="{custMarkup:InvokeCommand Command={Binding MyCommand}, CommandParameter=Whatever}" />

17/09/2011

Roslyn project CTP is almost here!

Filed under: roslyn — Ivan Danilov @ 06:54

For those of you who didn’t hear of Roslyn project before – in short it is compiler-as-a-service or CaaS for short. The first thing came to my mind when I first saw this term is that I have code in string and I can compile it. And it is wrong. Well, sort of. Compiling of the code is just the tip of an iceberg – and in fact it was available already. What CaaS really is it is possibility to interact with compiler, to work with its AST, to embed and interleave code (DSL, huh?). What’s it for? Well, carry on reading.

The great thing – Roslyn is announced. The second, more great thing – it will be here in four weeks, somewhere in the middle of October.

Those and other things is in Anders Hejlsberg’s talk on BUILD conference here. Entire talk is very interesting, but Roslyn details are from 35:15 and forward.

What Anders had shown in his talk… REPL loop (similar to the one he shown on PDC2008 but more immersive), simple refactoring (not using but writing one) and really crazy stuff – copying C# code and pasting VB code (and vice verse). All of that within Visual Studio with all of its goodies.

I’m really fascinated with all of these things and cannot wait to try them! This talk is a must see for every .NET developer IMO.

Create a free website or blog at WordPress.com.