MVVM : Cinch

Cinch is a free open source MVVM framework that I put together and will/has been released to the community. It has answers lots of the WPF/MVVM framework issues I have had when doing MVVM.

Here is a list of what Cinch contains:

  • 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 a XAML FrameworkElement
    • Have a group of such ICommand/RoutedEvent Events for a single XAML FrameworkElement
  • Allows the ViewModel to determine is a Models data should be editable, the UI simple 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)
    • Event logger service
    • MessageBox service (which can be 100% emulated within unit tests, as if the user actually clicked a MessageBox button), thus enabling the ViewModel code to be traversed down the correct path
    • Open file service (which can be 100% emulated within unit tests, as if the user actually chose a file to open in the model OpenFileDialog), thus enabling the ViewModel code to be traversed down the correct path
    • Save file service (which can be 100% emulated within unit tests, as if the user actually chose a file to save in the model SaveFileDialog), thus enabling the ViewModel code to be traversed down the correct path
    • Popup window service, to control the showing/hiding and setup of Popup Window in WPF (which is a nightmare)
  • Threading helpers
    • Dispatcher extension methods to allow quick marshalling of a Action<T> to the correct UI Dispatcher
    • Application.DoEvents
    • Application.DoEvents (for a certain Dispatcher Priority)
    • BackgroundTaskManager with callback to alert waiting Unit tests of completion, to allow test to complete or timeout
    • ObservableCollection, which notifies CollectionChanged on correct Dispatcher thread
    • ObservableCollection, which allows a range of items to be added
  • MenuItem ICommand ViewModel implementation made easy
  • Closeable ViewModels (when working in Tabbed UI enviorment, which is what one should really be doing with MVVM)

Here are the links to Cinch that point to all the articles that describe how to get started with Cinch.

http://www.codeproject.com/KB/WPF/Cinch.aspx

http://www.codeproject.com/KB/WPF/CinchII.aspx

http://www.codeproject.com/KB/WPF/CinchIII.aspx

http://www.codeproject.com/KB/WPF/CinchIV.aspx

http://www.codeproject.com/KB/WPF/CinchV.aspx

http://www.codeproject.com/KB/WPF/CinchVI.aspx

Cinch V2 Articles

Discussed the use of MEF and the MefedMVVM library within CinchV2

Discussed the WPF/SL and common services within CinchV2

Discussed the new features of Cinch V2 that were not in Cinch V1

A Deep Dive Into What Has Changed/What Has Stayed The Same

Disecting The WPF Demo App

Disecting The SL4 Demo App

Community Contributions / Further Articles

Convert a Silverlight Navigation Application to use MVVM using Cinch V2

Cinch Working With PRISM

Showcasing Cinch MVVM framework / PRISM 4 interoperability

8 thoughts on “MVVM : Cinch

  1. Hello Sacha, I’ve been using your framework since version 1, but only on the wpf side.
    Now I’m in a Silverlight project and I can’t make the CanExecuteCommand change accordinly to the IsValid property.

    I tested it in your Silverlight demo and those are the only lines of code I changed:

    SaveUserNameCommand = new SimpleCommand(CanExecuteSaveUserNameCommand, ExecuteSaveUserNameCommand);

    and this is the implementation of the CanExecuteSaveUserNameCommand:
    private Boolean CanExecuteSaveUserNameCommand(Object args)
    {
    return IsValid;
    }
    The button never works.
    I guess I had too many beers, since I can’t imagine this is not working..
    Can you help me out?

    Another thing I noticed is that if I change your Silverlight project (CinchSL source) from Silverlight 4 to 5 it fails to compile on this line:
    DynamicMethod dm = new DynamicMethod(
    “FastSmartWeakEvent”, typeof(bool), forwarderParameters, method.DeclaringType);
    since in version 5 there is not anymore this constructor.
    Are you planning to update your Cinch framework to Silverlight 5?

    Roberto Dalmonte

  2. Hi Sasha

    I have been looking for a simple no mess no fuss framework with which to dev WPF desktop apps and pretty much like CinchV2. So I downloaded your CinchV2AndPrismModulesRegions from CodeProject.

    I tried to use it with Prism V5 and Prism Mef Extensions with .NET v4.5 and came across some problems.

    The first one was that QueryUri has been renamed to NavigationParameters:

    var parameters = new NavigationParameters {{“FirstName”, currentDisciple.FirstName}};
    _regionManager.RequestNavigate(Regions.MainRegion, new Uri(“DetailView” + parameters.ToString(), UriKind.Relative), NavigationComplete);

    Now, that I have it compiling when I run, the bootstrap loader fails with ReflectionTypeLoadException

    Bootstrapper b = new Bootstrapper();
    try
    {
    b.Run();
    }
    catch (ReflectionTypeLoadException ex)
    {
    foreach (var item in ex.LoaderExceptions)
    {
    MessageBox.Show(item.Message.ToString());
    }
    }

    The underlying problem is that it cannot load the assembly(s) System.Windows.Interactivity v4.5 or v4.0 depending on whichever I change it to.

    Have you come across this problem before? If so how did you resolve it?

    1. Yeah that is more than likely down to a PRISM code error (your code basically), sadly I will not be able to help with that. That Navigation thing is part of PRISM not cinch.

      Sorry

      1. Sadly, not my code.. It’s your code I was using but linked with Prism V5 instead.

        Does this mean CinchV2 is no longer supported going forward? i.e. If I wanted to adopt it for use in my LOB apps this might not be a good idea (unless I clone your current codebase and fix ir on codeplex).

        Or would you rather recommend simply using Prism V5?

        Thanks for your reply though

  3. Hi Sacha,

    Can I create a SimpleRule for a Boolean field(which cannot be used as a DataWrapper)?

Leave a comment