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!