Cinch MVVM Framework by Anton


You already know a lot of MVVM frameworks. But this one is came from WPF technology.

Cinch is a WPF ModelView-ViewModel framework that takes the UI services route to provide a rich WPF MVVM framework that comes with several standard services out of the box. As it was mentioned before it was firstly developed as WPF framework and then were extended to the Silverlight.

Cinch V1 is aimed at VS2008, though should work with VS2010 also. It’s official information, but let’s move closer to the features:

  • It’s allows view to communicate LifeCycle events to a ViewModel without any hard reference links being maintained, and no IView interface requirements. There is no link at all between the View and the ViewModel.
  • Has several attached behaviours for common tasks such as:
    • Numeric text entry
    • Run an ICommand in a ViewModel based on a RoutedEvent from an XAML FrameworkElement
    • Have a group of such ICommand/RoutedEvent Events for a single XAML FrameworkElement
  • Allows the ViewModel to determine if a Models data should be editable, the UI simply updates via bindings, based on the ViewModel driven editability state. This is available at individual Model field level, so it’s very flexible.
  • Delegate validation rules which allows validation rules to be as granular as necessary
  • Native IDataErrorInfo support using the Delegate rules approach
  • IEditableObject usage to store/restore object state on edit / cancel edit
  • Weak event creation, to allow the creation of WeakEvents
  • Weak event subscription, which also allows auto unsubscriptions
  • Mediator messaging with WeakReference support out of the box
  • DI/IOC using Unity DI Container, to allow alternative Test/actual app service implementations
  • Service implementations which include (where Cinch has defaults for WPF/UnitTest for most of these, with the exception of the Popup window service which will be covered in a subsequent article and shall still be available within the attached demo code at any rate) such as Event logger, MessageBox service, Open and save file services and popup window services. The last is most attractive, because it’s hard to implement normal popup window in WPF.
  • Threading helpers to work with Dispatcher, BackgroundTaskManager and ObservableCollection.
  • MenuItem ICommand ViewModel implementation made easy
  • Closeable ViewModels (when working in Tabbed UI environment, which is what one should really be doing with MVVM)

 

All this methods is documented very well, but be sure that you are using it in correct projects. Some of the behaviors are WPF only. Like TextBoxFocusBehavior, SelectorDoubleClickCommandBehavior, NumericTextBoxBehaviour. In this article you can find all needed information.

What else can be useful to Silverlight developers? SimpleCommand is the answer. This class is based on Func<t, tresult=”">delegate. Encapsulates a method that has one parameter and returns a value of the type specified by the TResult parameter. It’s often used when you want to control the execution of specific functions or procedures. Like in web requests – where you have to wait some time before proceed with the new one. This class is a wrapper made to implement this behavior.

// 
/// Simple delegating command, based largely on DelegateCommand from PRISM/CAL
/// 
/// The type for the ICommand.CanExecute() parameter
/// The type for the ICommand.Execute() parameter
public class SimpleCommand<t1,t2> : ICommand, ICompletionAwareCommand
{
private Func<t1, bool=""> canExecuteMethod;
private Action executeMethod;
private WeakActionEvent<object> commandCompleted;
public SimpleCommand(Func<t1, bool=""> canExecuteMethod, Action executeMethod)
{
this.executeMethod = executeMethod;
this.canExecuteMethod = canExecuteMethod;
this.CommandCompleted = new WeakActionEvent<object>();
}
public SimpleCommand(Action executeMethod)
{
this.executeMethod = executeMethod;
this.canExecuteMethod = (x) => { return true; };
this.CommandCompleted = new WeakActionEvent<object>();
}        
public WeakActionEvent<object> CommandCompleted { get; set;}       
public bool CanExecute(T1 parameter)
{
if (canExecuteMethod == null) return true;
return canExecuteMethod(parameter);
}
public void Execute(T2 parameter)
{
if (executeMethod != null)
{
executeMethod(parameter);
}
//now raise CommandCompleted for this ICommand
WeakActionEvent<object> completedHandler = CommandCompleted;
if (completedHandler != null)
{
completedHandler.Invoke(parameter);
}
}
public bool CanExecute(object parameter)
{
return CanExecute((T1)parameter);
}
public void Execute(object parameter)
{
Execute((T2)parameter);
}

We can see that there are Func<t1, bool=”"> and Action which are helping to execute some function by it’s wrapped pointer. You can encapsulate any procedure and function and execute anytime you need. More about function pointers and delegates you can find on MSDN websites.

Project hosted at Codeplex: http://cinch.codeplex.com/

Demo Silverlight application: http://www.codeproject.com/KB/WPF/CinchV2_6.aspx

It’s almost incredible but demo project has it’s own documentation with patterns pictures and code explanation. It’s really explains everything to start developing from the scratch.