Getting Started - Sebazzz/SDammann.WindowsPhone.MyMvvm GitHub Wiki

Since most of the documentation still needs to be written, I've created a guide to help you on your way. This guide will not explain view models, but will only help you on your way with MyMvvm.

Set-up your IoC container

This step is optional, but is strongly recommended. This step actually consists of two sub-steps:

Choose and add an IoC

First choose your IoC container, I recommend Ninject as I have build Ninject support for MyMvvm. However, MyMvvm is able to use any dependency injection container. I recommend to choose a IoC container with injection support like Ninject has.

Create and add the DI container project

You need to implement the IServiceProviderEx interface to add support for the DI container. You then use the ServiceResolver.SetServiceResolver(IServiceProviderEx) method in your Application definition constructor (App.xaml.cs) to register your DI container.

Create a ViewModel class

First, create a 'ViewModel' folder in your project. This folder will contain all your ViewModels. Then, create a ViewModel class for your page. The view model class must derive from the PageViewModelBase class. The class will require you to implement the OnSaveState and OnRestoreState methods, these methods are required for thombstoning support.

Create a Page

Next, create a page that is associated to your view model. The page needs to be derived from the ViewModelPhonePage class. Example:

<myMvvm:ViewModelPhonePage
    x:Class="MyProject.PhoneApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:myMvvm="clr-namespace:SDammann.MyMvvm.Phone;assembly=SDammann.MyMvvm.Phone" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:ViewModel="clr-namespace:MyProject.PhoneApp.ViewModel" mc:Ignorable="d">

  <!-- Paste something like the following in phone pages -->
  <myMvvm:ViewModelPhonePage.Resources>
    <ViewModel:MainPageViewModel x:Key="ViewModel" d:IsDataSource="true" />
  </myMvvm:ViewModelPhonePage.Resources>
  <myMvvm:ViewModelPhonePage.DataContext>
    <Binding Source="{StaticResource ViewModel}"/>
  </myMvvm:ViewModelPhonePage.DataContext>

</myMvvm:ViewModelPhonePage>

In your code behind file, create an accessor for your view model. You can use this for when you cannot use an ICommand directly but need to route some events manually.

    /// <summary>
    ///   Gets the view model for this page
    /// </summary>
    private MainPageViewModel ViewModel {
        [DebuggerStepThrough]
        get { return (MainPageViewModel) this.ViewModelBase; }
    }

Fill up your view model

Last and the next steps are to fill up your page and view model. You can use the IoC container ability to inject instances. For design-time data, use the constructor, and check the IsDesignTime property. Examples are below:

    [Inject] // inject using DI container (Ninjects InjectAttribute)
    [EditorBrowsable(EditorBrowsableState.Advanced)] // hide it from Expression Blend
    public IAdvancedComponent Engine { get; set; }

    /// <summary>
    /// Initializes a new instance of the <see cref="MainPageViewModel"/> class.
    /// </summary>
    public MainPageViewModel() {
        // TODO: do common initialization

        // set-up design time data if necessary
        if (IsInDesignMode) {
            // TODO: do design-time initialization
        }
    }

You can override some methods of the PageViewModelBase class to control the page life cycle, some of the methods include OnPageLoaded, OnPageRefresh and OnBackKeyPress.

It is recommended to use something like NotifyPropertyWeaver to automatically weave property change handlers into your automatic properties.

⚠️ **GitHub.com Fallback** ⚠️