.NET Zone is brought to you in partnership with:

Michael Crump is a Microsoft MVP, INETA Community Champion, and an author of several .NET Framework eBooks. He speaks at a variety of conferences and has written dozens of articles on .NET development. He currently works at Telerik with a focus on our XAML control suite.
You can visit his blog at: MichaelCrump.Net or follow him on Twitter at: @mbcrump

Michael is a DZone MVB and is not an employee of DZone and has posted 103 posts at DZone. You can read more from them at their website. View Full User Profile

Setting up a Command (MVVM) in Windows Phone 8 Video Tutorial

04.27.2013
| 3129 views |
  • submit to reddit

Introduction

I hear this question a lot.. Should I use code-behind or MVVM in my application? What are the pros/cons of each and can you show me a simple example of how a Command works?

  • With Code-Behind – Your page is tightly coupled with your UI, testing is harder, it gets really messy with larger apps. On the other hand, smaller apps with just a couple of screens, it may make sense.
  • With MVVM  – You can make drastic changes to the UI, with no impact to the data and business logic (true separation of concerns), easier to test, not necessary IMHO for smaller apps with just a couple of screens.

I could go a lot deeper with this topic but won't at this time. I’d really encourage you to use your Google-Fu to do more in-depth research on this.

Others may agree or disagree with me regarding smaller apps, but hey, that is my opinion. :) I’m also not a purist – meaning that I don’t find a problem with having some code-behind in my MVVM apps. I guess that counts as another strike against me! Dang it!

A Simple Example of Setting Up a Command

Anyways, I decided to try out CodeProject.TV to launch a simple example called, “Setting up a Command (MVVM) in Windows Phone 8”.  In this video, I show you how Commands work for projects that use MVVM (Model View View-Model). I will take a simple button and add a Command to it to display a MessageBox. This same code would work with other XAML platforms, such as WPF, SL, Windows Phone 7.x and Windows Store Apps (except the XMLNS declaration).

Let me know if you need any help and please vote up the video!

Published at DZone with permission of Michael Crump, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)