As the primary aim of MVVM cross is to supply a cross platform MVVM framework, our primary goal must be to try to ensure that all cross platform compatible code is separated from non-cross platform. Ideally all non-UI code would be cross platform in a perfect world.
So this and any other posts on MVVM cross will assume and aim to ensure that the non-UI code is as portable as possible between platforms. The example code etc. will assume that Xamarin tools have been installed in Visual Studio, but this is not a requirement for the examples to work.
Creating the view model
- So to begin with we need to create a PCL (Portable Class Library). Give the project a name (i.e. I named mine Common) and then press OK, then select all the platforms you want the library to be compatible with – as I have Xamarin tools for Visual Studio installed the selected platforms include Windows, Windows 8, Windows Phone 8, Xamarin.Android and Xamarin.iOS
- Delete Class1.cs as we’re not going to be needing it
- Using NuGet, add MVVM cross to the project
- Now, MVVM cross uses IoC and a naming based convention approach to wiring views and view models together, so you’ll notice that by default NuGet created a ViewModels folder with a FirstViewModel.cs. Whilst you can move the location or change the namespace of the view model file, it’s name and that of the corresponding view need to map to one another. Like Caliburn Micro a XXXViewModel is mapped to an XXXView.
- For this sample we’ll implement the FirstViewModel implemented in Stuart Lodge’s N+1 youtube video on using MVVM cross. So edit the FirstViewModel to look like the following
public class FirstViewModel : MvxViewModel { private string firstName; private string lastName; public string FirstName { get { return firstName; } set { firstName = value; RaisePropertyChanged(() => FirstName); RaisePropertyChanged(() => FullName); } } public string LastName { get { return lastName; } set { lastName = value; RaisePropertyChanged(() => LastName); RaisePropertyChanged(() => FullName); } } public string FullName { get { return String.Format("{0} {1}", firstName, lastName); } } }
Note: Our view model inherits from MvxViewModel
And that’s it for now.
So to review the above, we create a PCL library, add the MVVM cross assemblies (in this case we used NuGet), derive any view models from MvxViewModel and we’re done.
Note however that we kept the default FirstViewModel class, had we changed this name we’d also need to change the code in App.cs to reflect this change. The view model we created is basically the app view model so it’s registered in the App.cs file. But you’d get a compile time error if you didn’t make this change so that’s easy to fix.
Creating the view
Whilst it’s probably more interesting to now create an iOS or Windows Phone 8 app. I want to start with a WPF application, so here’s the process for creating a WPF app and connecting it to our view model.
- Create a new WPF Application project (I’m using the same solution as my PCL project created above)
- I’m set this project as the start StartUp project
- Using NuGet add MVVM cross to the references
- Add a reference to our PCL project
- Now remember that the view and view model are mapped by a naming convention, so in the Views folder you should find FirstView.xaml. Notice the view is an MvxWpfView, so similarly new views should also be derived from this class.
- Again, as per Stuart Lodge’s N+1 video we’ll simply add the following to the XAML for the FirstView.xaml
<TextBox Text="{Binding FirstName, Mode=TwoWay}"/> <TextBox Text="{Binding LastName, Mode=TwoWay}"/> <TextBlock Text="{Binding FullName, Mode=OneWay}"/>
-
Finally and before we can run this code. I named my PCL assembly Common and thus we need to change the Setup.cs file to reflect this. the method CreateApp needs to look like this
protected override IMvxApplication CreateApp() { // uses the App object in our PCL assembly return new Common.App(); }
Now this should run – no need to assign any views to the MainWindow, MVVM cross will handle this for you based upon your App class in the PCL assembly.