Feeds:
Posts
Comments

Archive for August, 2007

ViewModelCommand

I don’t know if I should be excited by my newest discovery, or utterly annoyed.

You see, I wrote a while ago about the Model-View-ViewModel pattern, as blogged by Dan Crevier.  He had encapsulated all actions that would be initiated in the View by the user into the ViewModel by creating what he called a CommandModel.  The CommandModel used a RoutedCommand internally and you’d derive new CommandModels that would delegate command processing to the ViewModel.  Then a CreateCommandBinding attached property would create the command bindings necessary to hook up the internal RoutedCommand.  I disliked this implementation for two reasons.  First, you had to derive a new CommandModel for every single command and this derived class had tight coupling to the specific ViewModel.  This seemed like overkill.  This problem was easy to correct by inverting the pattern and using a single CommandModel that you didn’t inherit from, but instead when you instantiated it you provided callbacks for the executed and can execute events.  The second issue was a bit tougher, and I didn’t resolve it in the original post.  The CreateCommandBinding was necessary to wire up the RoutedCommand, but it resulted in verbose and basically duplicated code in the XAML.  For example, here’s how you’d code up a command in the XAML using Dan’s design.

<button loc:CreateCommandBinding="{Binding Path=AddCommand}" Command="{Binding Path=AddCommand.Command}"/>

Well, I didn’t like this, but fixing it was proving to be a bit of a pain.  I eventually came up with a MarkupExtension that could be used like in the following.

<button Command="{loc:Command Path=AddCommand}"/>

I didn’t care for the code in this MarkupExtension, but it worked.  I was all ready to share this with the world (in fact, I did share it with people at work), when a coworker sent me some links to other blog posts about this topic (thanks, Roger).  There was a specific post by John Gossman that I took notice of.  He had very similar code to Dan, but I noticed that in his XAML he wasn’t using a CreateCommandBinding or anything similar.  So, how did it work?  Well, looking closer at the code, I discovered that his CommandModel (he used a different name) didn’t use a RoutedCommand internally.  Instead, it derived directly from ICommand!  There was the key.  If you don’t use a RoutedCommand, there’s no need to use command bindings to hook up event handlers.  The ICommand interface is your gateway into the event handling.  John still used a derived CommandModel for every command with tight coupling to the ViewModel, but it really doesn’t take much to invert this pattern in the same way we did previously (the code for my new ViewModelCommand is so simple that I’ll leave it as an exercise for the reader, unless someone begs me to provide it).  Now the wiring in XAML just looks like the following.

<button Command="{Binding Path=AddCommand}"/>

Now this I like.  A lot.  But I feel stupid for having spent so much time creating my custom MarkupExtension here.  I suppose there may some day be a need for a RoutedCommandModel, in which case that code will be dusted off.  But honestly, given the use case for CommandModels, I’m not at all sure that will ever be the case.  Only time will tell.

Advertisements

Read Full Post »

3D Programming for Windows

A while back, Charles Petzold posted a blog entry asking for people to e-mail him if they had a blog on which they discussed WPF.  I had no idea what it was about, and I was quite aware that my own blog is completely unknown and has little content so far.  But I figured what the heck, and sent him an e-mail.  Then I completely forgot about it.

Well, last week my wife called and asked if I was expecting anything in the mail from someone in NY.  Turns out, he sent me an autographed copy of 3D Programming for Windows.  I figure I owe him something, and the least I can do would be to review the book.

I have to admit, this is a topic that I don’t have a ton of interest in.  Combine that with having little free time right now, and it might take me a while to read the book thoroughly enough for a full review.  However, I’ve read several chapters and have skimmed the rest of it, so I think I know enough for a preliminary review.

This is most certainly an advanced book.  There’s as much coverage on mathematics as there is on the topic of the 3D interfaces in WPF.  Mr. Petzold appears to have done a fair job of explaining the math, though this is certainly not a book for entry level knowledge about that topic.  He seems to have done a good job with the organization of this book as well (one of my criticisms with Applications = Code + Markup).  There’s also plenty of illustrations (a complaint that others had of A=C+M), though they are still only black and white (yet another complaint others had of A=C+M).  Personally, I don’t have any issues with the illustrations being black & white, and didn’t really have an issue with the number of illustrations in A=C+M, and in this book there are definitely plenty enough illustrations.  More interestingly, every illustration was done using XAML and they are available for download in that format.

Over all, this seems to be a book worth recommending.  The only criticism I have of it at this point is with the lack of real world examples.  Mr. Petzold makes it very clear in numerous places in the book that 3D in WPF is about interfaces and data visualizations, and not about games or movie animations, yet the vast majority of the book uses academic 3D visualizations instead of interfaces or data visualizations.  In fact, there are no real world examples until the end of the book, where he dedicates an entire chapter to this topic.  However, even in this chapter there’s only about 3 examples.  This isn’t a very big criticism, and some may even consider it to be unfair, but for someone like me, who’s wondering why we need 3D in our UIs in the first place, the lack of more concrete examples certainly doesn’t help.

That said, if you’re at all interested in this topic, this may indeed become the definitive book on the topic.  At the very least, it will be in the top rung and is well worth the read.

Technorati Tags: ,

Read Full Post »