MVVM your Kinect code

MVVM is a pattern that I'm a fan of. It's a solid step toward developing application UI's that are easier to maintain and test by removing, eliminating the need for, "code behind" code. If you've ever had to maintain any app of any complexity over a period of time that relied on code behind, you'll know what I mean when I say that a real, reliable and "standard" replacement pattern is very welcome.

Now lets mix in Kinect and MVVM and let's see what we get...

Kinect Sensor in an MVVM App

Now that the official Kinect SDK is out I've been looking for good patterns when incorporating it into Windows 7 applications. The Model-View-ViewModel (e.g. MVVM) pattern is my pattern of choice for WPF applications and I wanted to demonstrate how I incorporate the Kinect runtime within the MVVM pattern.

I've created a screencast walk-through of how you can incorporate the Kinect runtime into MVVM (or read on below for a brief description of the approach).

Project Information URL: http://kindohm.com/2011/07/20/KinectMvvm.html

Project Source URL: https://github.com/kindohm/kinect-mvvm

public class ViewModelLoader
{
    static ViewModel viewModelStatic;
    static IKinectService kinectService;

    public ViewModelLoader()
    {
        kinectService = new ConcreteKinectService();
        var isInDesignMode = ...; // detect if running in VS designer
        if (!isInDesignMode) kinectService.Initialize();
    }

    public static ViewModel ViewModelStatic
    {
        get
        {
            if (viewModelStatic == null)
            {
                viewModelStatic = 
                    new ViewModel(kinectService);
            }
            return viewModelStatic;
        }
    }

    public ViewModel ViewModel
    {
        get
        {
            return ViewModelStatic;
        }
    }

    public static void Cleanup()
    {
        if (viewModelStatic != null)
        {
            viewModelStatic.Cleanup();
        }
        kinectService.Cleanup();
    }
}

Contact Information:

Follow the Discussion

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.