Thomas
Thomas

Reputation: 34218

Basic guidance require for MVVM design pattern

i am not familiar with MVVM design pattern. so reading article on it from a url http://www.geekchamp.com/articles/windows-phone-mango-getting-started-with-mvvm-in-10-minutes

here is a small code of MVVM design pattern

public class Person : INotifyPropertyChanged
{
    private string name;
    private int age;

    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            if (this.name != value)
            {
                this.name = value;
                this.RaisePropertyChanged("Name");
            }
        }
    }

    public int Age
    {
        get
        {
            return this.age;
        }
        set
        {
            if (this.age != value)
            {
                this.age = value;
                this.RaisePropertyChanged("Age");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

1) i like to know what is the use of PropertyChangedEventHandler ?? what it does?

2) i like to know what is this.RaisePropertyChanged("Age"); is doing ?

please explain it in easy way so i can understand their use and importance.

thanks

Upvotes: 2

Views: 69

Answers (2)

Eric Scherrer
Eric Scherrer

Reputation: 3410

1) What is the use of PropertyChangedEventHandler ?? what it does?

PropertyChangedEventHandler "represents the method that will handle the PropertyChanged event raised when a property is changed on a component." That line of code declares an event called PropertyChanged. Other objects can subscribe to that event to be notified when a property has changed.

2) What is this.RaisePropertyChanged("Age"); is doing ?

this.RaisePropertyChanged("Age") is a call to the RaisePropertyChanged method, which in turn verifies that there are subscribers to the event (handler != null) and if so then notifying them of the change (handler(this, new PropertyChangedEventArgs(propertyName))).

In short it is a pub/sub notification system for one object publishing to it's subscribers that something has changed.

Upvotes: 1

Belogix
Belogix

Reputation: 8147

These two things allow your Person class to inform the user interface (i.e. the View in MVVM) that something has changed.

So:

PropertyChanged is an Event (of type PropertyChangedEventHandler) that the View can subscribe to and when triggered might do "something".

RaisePropertyChanged is a method that you call within the class to say "Hey, I have changed and fire the event (as mentioned above) please".

So, say your person's name changes then the PropertyChanged event will be raised and the View will then be notified that something has happened that it might be interested in. The whole point is the class needs some way to tell the rest of the world that something has happened. It does this by Events. Of course, everyone might ignore that event, but by raising it you have done your part.

Upvotes: 1

Related Questions