Nano ViewModel Support

Nano view model is very small library to build MVVM enabled Silverlight/WPF/WP7 based application.MVVM becomes more and more popular in fast few years and there are many implementations  like MVVM Light toolkit, Prism(Last Version), nRoute, or Caliburn however these libraries have too many features and keep on growing which could have adverse effect on you project in terms of performance etc.For example communication between view in MVVM Light Toolkit is based on silverlight messaging which may not be best if you working ithin single silverlight application.It can be used when you page have more than one silverlight application (xap files) which requires communication and what about if you are not intead to use some of the feature from the toolkits i.e you want just the MVVM but you end up with adding complete set of library in your project.What if you need some features from MVVM Toolkit and you too want to use prism library.

There are a number of benefits this pattern brings to both WPF and Silverlight development. Before you go on, ask yourself:

  • Do you need to share a project with a designer, and have the flexibility for design work and development work to happen near-simultaneously?
  • Do you require thorough unit testing for your solutions?
  • Is it important for you to have reusable components, both within and across projects in your organization?
  • Would you like more flexibility to change your user interface without having to refactor other logic in the code base?

MVVM is not just about resuability,blendability & testability however its very power and if used properly you could create low latency WPF based applications.If you want to know about the theory you may follow the post here or google it and you will find many discussions and articles on MVVM.

Below are the features supported by NanoVmSupport library

  • Minimum code in you view model.
  • No nead to have obvervable properties and extra code with each property.
  • Supports command implementation.
  • Command binding for all most all controls using EventToCommand implementation
  • Provision to set design time data for view model (supports blendability)
  • NanoVmSupport.Events libaray supports communication between two view models in you application.It is extracted from Prism library so you if you are familiar with prism its very easy for your.Please note that you don’t have to use prism to use this.If few months later support you want prism, you may exclude NanoVmSupport.Events library and your code will start using eventing from prism with just chnaging single line of initilization.
  • You can easyly write unit tests against your view models.

The sample silverlight application below (MVVM & library MVVM.Events) is the implement of NanoVMSupport & NanoVMsupport.Events.The source code for sample project and NanoVMSupport library can be downloaded from here and if required binaries are here.
[silverlight: http://www.pixytech.com/rajnish/uploads/code/NanoVM/MVVM.xap,650, 550]

The sample folder in the source code also includes sample application code for silverlight and WPF.The WPF application is Auction Sniper for madbid which places your bids automaticaly based on some figures/process.
For communication between viewmodels check the documentation from prism or send me your queries in this post,for Microsoft Unity Application block click here.

MVVM common Issues & fixes in NanoVMSupport :

  • Standard Silverlight TextBox control is very useful but has one strange behavior: if you use TwoWay data binding and bind some property to controls Text property, when users type text into the control, this change is not propagated to the bound property until the control loses its focus. The AutoComit Behavior for textbox control is now part of NanoVMSupport Library (Lib for MVVM) for detail check this article “Silverlight TextBox AutoComit Behaviour“.
  • How to invalidate the CanExecute value : Suppose you have textbox and command button ,Can execute returns false if the length of text is zero otherwise it returns true and you have also used the above mentioned Auto Comit behaviour on text box.CanExecute will be re-evaluated when something interesting within the application’s window happens in WPF however in silverlight or with NanoVMSupport library you have to explictly update the ICommand.The one way of doing this is to change “RaisePropertyChange” method in ViewModelBase.cs as mentioned below
    protected void RaisePropertyChanged(string name)
    {
        PropertyChanged.Raise(this, name);
        PropertyChanged.Raise(this, "");
    
    }

    The code line PropertyChanged.Raise(this, “”); will refresh all the properties and icommands on view.But it may have performance issues if you have may properties and commands in viewmodal.The another recommended approach is to use following code in viewmodal

    public string SelectedText
    {
        get { return Get(() => SelectedText); }
        set
        {
            Set(() => SelectedText, value);
            base.RaisePropertyChanged("SearchCommand");
        }
    }

    The code base.RaisePropertyChanged(“SearchCommand”); will invalidate the SearchCommand.

3 thoughts on “Nano ViewModel Support

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s