Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Observing objects

In order for the view to get notified about changes to the view model, the view model should implement INotifiedPropertyChanged. In fact, if the view model does not implement this, it could lead to memory leaks.

With a BaseViewModel we can abstract this out and reuse it when creating new view models.

A typical case when dealing with notification is when changing a property. With this base view model you could do the following:

namespace observingobjects
{
    public class FooBarViewModel : BaseViewModel
    {
        private string m_bar;

        public string Bar
        {
            get => m_bar;
            set => SetProperty(ref m_bar, value);
        }

        public void Foo()
        {
            Bar = "New value";
        }
    }
}

Code explanation:

  • We have created a field to store the value of Bar in.
  • When we call Foo() we change the value of m_bar through Bar by calling SetProperty().
  • SetProperty() is a method that comes from BaseViewModel and that does a equality comparing.
  • If the value has been changed, it will notify property changed. That way, the UI will update its value.

The code for FooBarViewModel can be found here.

The code for BaseViewModel can be found here