Posted by: Zeeshan Amjad | March 14, 2011

Define base class for ViewModel


We saw few examples of using MVVM here. We noticed that we have to write similar code again and again and now we are tying to automate this process. In this step we are going to define one small base class that define minimal detail. In first step we are going to use INotifyPropertyChanged interface in our ModelView base class. In addition we define one ICommand property to close the View. When we inherit our class from this then we have some basic functionality of ViewModel. We also define one public event to handle the close functionality. We also make this class abstract, so it can’t be use directly and we have to inherit class from it. Here is our simple implementation of ViewModel base class.

Code Snippet
public abstract class ViewModelBase : INotifyPropertyChanged
{
    public event EventHandler RequestClose;

    public ICommand ExitCommand
    { get; set; }

    public void Close()
    {
        EventHandler handler = this.RequestClose;

        if (handler != null)
        {
            handler(this, EventArgs.Empty);
        }        
    }

    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

 

Now we if we inherit a class from it, then that class has some basic functionality of ViewModel.

Code Snippet
// this class has minimal ViewModel support
public class MyViewModel : ViewModelBase
{    
    // other stuff
}

 

We have to write one line to implement the Close functionality. Here is our code.

Code Snippet
// this class has minimal ViewModel support
public class MyViewModel : ViewModelBase
{
    public MyViewModel()
    {
        ExitCommand = new MyCommand(Close);
    }
    // other stuff
}

 

Here MyCommand is simple implementation of ICommand interface.

Code Snippet
public class MyCommand : ICommand
{
    public Action Function
    { get; set; }

    public MyCommand()
    {
    }

    public MyCommand(Action function)
    {
        Function = function;
    }

    public bool CanExecute(object parameter)
    {
        if (Function != null)
        {
            return true;
        }

        return false;
    }

    public void Execute(object parameter)
    {
        if (Function != null)
        {
            Function();
        }
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }
}

Advertisements

Responses

  1. […] simple approach is to introduce event properties in ViewModel and set it from the view as we saw in this example. Another approach is to convert event into command and use the command binding. In this […]

  2. […] discussed the simple base class for smple ViewModel here. Now we are going to extend this concept. This time we are going to implement the disposable […]


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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Categories

%d bloggers like this: