I love Model View ViewModel (MVVM) pattern when doing Windows Phone or Windows 8 development. It allows nice separation of UI and code which gives your project very good structure. There’s basically two common MVVM toolkits out which are MVVM Light and Caliburn Micro.
I first used MVVM light heavily but have recently moved over into Caliburn Micro. Biggest difference is that caliburn uses naming convention while mvvm light doesn’t. Using naming conventions makes you fast but it also raises the barrier if someone not familiar with the pattern is looking at your code.
Considering the facts I do recommend using Caliburn Micro and here’s sample how to get started and Build Windows Phone 8 hello world with Caliburn Micro.
- Create new Windows Phone 8 basic project
- Install Caliburn.Micro.Start nuget package
- Create new folder into your project "Views"
- Create new folder into your project "ViewModels"
- Drag MainPage.xaml and MainPageViewModel.cs to their folders
- Change Navigation Page from WMAppManifest.xml to "Views/MainPage.xaml"
- Remove everything else than InitializeComponent() from app.xaml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
using System.Windows; namespace TT_WP8_Caliburn_1 { public partial class App : Application { public App() { InitializeComponent(); } } } - Edit your app.xaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
<Application x:Class="TT_WP8_Caliburn_1.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ttWp8Caliburn1="clr-namespace:TT_WP8_Caliburn_1"> <Application.Resources> <ttWp8Caliburn1:AppBootstrapper x:Key="Bootstrapper" /> </Application.Resources> </Application> - Prepare your first viewmodel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
using System.Windows; using Caliburn.Micro; namespace TT_WP8_Caliburn_1.ViewModels { public class MainPageViewModel : Screen { public MainPageViewModel() { MessageBox.Show("Hello world! from MainPageViewModel"); } } } - Clean MainPage.xaml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
using Microsoft.Phone.Controls; namespace TT_WP8_Caliburn_1.Views { public partial class MainPage : PhoneApplicationPage { public MainPage() { InitializeComponent(); } } } - Enjoy your end results
If you have any questions regarding how to use Caliburn Micro with Windows Phone 8 or Windows 8 please do so on comments. Whole solution can be found here.