Reputation: 277
I am developing a Xamarin Forms app. I have a singleton class implemented in this way:
public class DataManager
{
static readonly Lazy<DataManager> _instanceHolder =
new Lazy<DataManager>(() => new DataManager());
public static DataManager Instance => _instanceHolder.Value;
//private constructor to prevent multiple instances of DataManager
private DataManager(){}
private UserModel _loggedUser;
public UserModel LoggedUser
{
get
{
return _loggedUser;
}
set
{
_loggedUser = value;
}
}
}
Then, in several class I "linked" the LoggedUser property of the Instance of DataManager in this way:
public class Test
{
public UserModel User
{
get
{
return DataManager.Instance.LoggedUser;
}
}
}
However, anytime I change the LoggedUser
property, both changing one of its property or reassigning a new object to it, changes are not reflected in the classes where LoggedUser
is used (in this case in the Test class
). How do I "retrigger" the get
once the LoggedUser Property change inside the DataManager
instance?
Upvotes: 0
Views: 61
Reputation: 7189
You will have to implement the NotifyPropertyChanged.
public class DataManager : INotifyPropertyChanged
{
// Singleton
private UserModel _loggedUser;
public UserModel LoggedUser
{
get
{
return _loggedUser;
}
set
{
_loggedUser = value;
OnPropertyChanged("LoggedUser");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged == null)
return;
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Upvotes: 1