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!
Software Developer (Senior)
Michael Crump is a Silverlight MVP and MCPD that has been involved with computers in one way or another for as long as he can remember, but started professionally in 2002. After spending years working as a systems administrator/tech support analyst, Michael branched out and started developing internal utilities that automated repetitive tasks and freed up full-time employees. From there, he was offered a job working at McKesson corporation and has been working with some form of .NET and VB/C# since 2003.
He has worked at Fortune 500 companies where he gained experience in embedded systems design and software development to systems administration and database programming, and everything in between.
His primary focus right now is developing healthcare software solutions using Microsoft .NET technologies. He prefers building infrastructure components, reusable shared libraries and helping companies define, develop and automate process standards and guidelines.
You can read his blog at: MichaelCrump.net
or follow him on Twitter at @mbcrump