What Changed
In v20.1, we removed the PropertyManager
class. The PropertyBag
object is moved to BindableBase
and is only instantiated when methods that store a property value by its name are used. If you store your property values in fields, no excessive objects will be created.
Reasons for Change
Prior to v20.1, instantiation of a single BindableBase object meant instantiation of 3 reference objects on the heap - BindableBase
, PropertyManager
, and the propertyBag
object inside PropertyManager
. This significantly increased memory usage when a lot (hundreds of thousands) of BindableBase
objects were instantiated. Due to excessive memory allocation, collection of garbage also took significant time when objects were created.
Impact on Existing Apps
In versions prior to 18.2, we suggested the use of the PropertyManager.GetProperty
and PropertyManager.SetProperty
methods to create custom methods with the CallerMemberNameAttribute attribute.
C#public abstract class BindableBaseEx : BindableBase {
PropertyManager propertyManager = new PropertyManager();
protected bool SetProperty<T>(T value, [CallerMemberName] string propertyName = null) {
return propertyManager.SetProperty(propertyName, value, () => RaisePropertyChanged(propertyName));
}
protected T GetProperty<T>([CallerMemberName] string propertyName = null) {
return propertyManager.GetProperty<T>(propertyName);
}
}
public class ViewModel : BindableBaseEx {
public string FirstName {
get { return GetProperty<string>(); }
set { SetProperty(value); }
}
}
These methods will no longer be compiled.
How to Update Existing Apps
In case you still use this class, replace these custom methods with the built-in GetValue
and SetValue
methods available in versions 18.2 and newer.
C#public class ViewModel : BindableBase {
public string FirstName {
get { return GetValue<string>(); }
set { SetValue(value); }
}
}