Setting up a project to use cinch

Get the latest build

Extremely simple really. Just download the latest version of Cinch (including source) from here : http://cinch.codeplex.com/SourceControl/list/changesets
After you extract it navigate to e.g.: cinch-55703\cinch\V2 (VS2010 WPF and SL)
Extract the "WPF_Demo.zip" file. 
Navigate to : WPF_Demo\CinchV2DemoWPF\Lib  . These are your pre built libraries.

Setup the project

Now create a new WPF Project. 
Copy and reference all the LIB files in your project. 
Add a reference to the MEF data composition library : "System.ComponentModel.Composition.dll" 

Using a View Model Locator

ViewModel File

At the top of your view model add the following using statements : 

using System.ComponentModel.Composition;
using Cinch;
using MEFedMVVM.ViewModelLocator;
using MEFedMVVM.Common;

Decorate your ViewModel with the correct name: 

[ExportViewModel("MainWindowViewModel")]
[PartCreationPolicy(CreationPolicy.NonShared)]
class MainWindowViewModel : ViewModelBase

XAML View File

Set the data context to the correct ViewModel instance for your View: 

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:CinchV2="clr-namespace:Cinch;assembly=Cinch.WPF"
xmlns:meffed="http:\\www.codeplex.com\MEFedMVVM"
meffed:ViewModelLocator.ViewModel="MainWindowViewModel"

All set. Enjoy!

PS: Important Notes: 

[PartCreationPolicy(CreationPolicy.NonShared)]
Means that only a single instance is always used for whenever you import it to a view. 
[PartCreationPolicy(CreationPolicy.Shared)]
Will create a new instance of the ViewModel for each view instance.  This is standard MEF :)



Comments