I was trying a code with Event raising. But every time when event should raise, if
statement is never true as PropertyChanged
is always null.
public delegate void MyEventHandler(object sender, String change);
public event MyEventHandler PropertyChanged;
protected virtual void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null) //It is always null here
{
PropertyChanged(this, propertyName);
}
}
This is how I'm subscribing to this event:
data.facebook.PropertyChanged+=new Facebook.MyEventHandler(Facebook_PropertyChanged);
data.PropertyChanged+= new RootObject.MyEventHandler( data_PropertyChanged);
This is the class
public class Facebook
{
public Facebook()
{
_commentsbox_count = 0;
_comment_count = 0;
_click_count = 0;
_like_count = 0;
_share_count = 0;
_total_count = 0;
}
double _commentsbox_count;
double _click_count;
double _total_count;
double _comment_count;
double _like_count;
double _share_count;
public double commentsbox_count
{
get {
return _commentsbox_count;
}
set {
_commentsbox_count = value;
NotifyPropertyChanged("commentsbox_count");
}
}
public double click_count {
get
{
return _click_count;
}
set
{
_click_count = value;
NotifyPropertyChanged("click_count");
}
}
public double total_count {
get
{
return _total_count;
}
set
{
_total_count = value;
NotifyPropertyChanged("total_count");
}
}
public double comment_count {
get
{
return _comment_count;
}
set
{
_comment_count = value;
NotifyPropertyChanged("comment_count");
}
}
public double like_count {
get
{
return _like_count;
}
set
{
_like_count = value;
NotifyPropertyChanged("like_count");
}
}
public double share_count {
get
{
return _share_count;
}
set
{
_share_count = value;
NotifyPropertyChanged("share_count");
}
}
public delegate void MyEventHandler(object sender, String change);
public event MyEventHandler PropertyChanged;
protected virtual void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, propertyName);
}
}
}
public class RootObject
{
public RootObject()
{
_stumbleUpon = 0;
_twitter = 0;
_reddit = 0;
_pinterest = 0;
_linkedin = 0;
_googleplusone = 0;
_diggs = 0;
_delicious = 0;
_buzz = 0;
}
public Facebook facebook=new Facebook();
double _stumbleUpon;
double _reddit;
double _delicious;
double _googleplusone;
double _buzz;
double _twitter;
double _diggs;
double _pinterest;
double _linkedin;
public double StumbleUpon {
get
{
return _stumbleUpon;
}
set
{
_stumbleUpon = value;
NotifyPropertyChanged("StumbleUpon");
}
}
public double Reddit {
get
{
return _reddit;
}
set
{
_reddit = value;
NotifyPropertyChanged("Reddit");
}
}
public Facebook Facebook {
get
{
return facebook;
}
set
{
facebook = value;
}
}
public double Delicious {
get
{
return _delicious;
}
set
{
_delicious = value;
NotifyPropertyChanged("Delicious");
}
}
public double GooglePlusOne {
get
{
return _googleplusone;
}
set
{
_googleplusone = value;
NotifyPropertyChanged("GooglePlusOne");
}
}
public double Buzz
{
get
{
return _buzz;
}
set
{
_buzz = value;
NotifyPropertyChanged("Buzz");
}
}
public double Twitter
{
get
{
return _twitter;
}
set
{
_twitter = value;
NotifyPropertyChanged("Twitter");
}
}
public double Diggs
{
get
{
return _diggs;
}
set
{
_diggs = value;
NotifyPropertyChanged("Diggs");
}
}
public double Pinterest
{
get
{
return _pinterest;
}
set
{
_pinterest = value;
NotifyPropertyChanged("Pinterest");
}
}
public double LinkedIn
{
get
{
return _linkedin;
}
set
{
_linkedin = value;
NotifyPropertyChanged("LinkedIn");
}
}
public delegate void MyEventHandler(object sender, String change);
public event MyEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, propertyName);
}
}
}
This is how am initializing the data
RootObject data = new RootObject();
What is the possible reason for this?
Update
Ok i figured this out. when the values are assigned to data, a temporary Facebook object is created. it is then copied to data.Facebook (copy constructor). That is why that temporary Facebook object is not subscribed and gives null in the event. As it is copied using copy constructor. no NotifyPropertyChanged is fired in data.facebook. Now i have narrowed this down. But i dont know what is the work around for this. Any advice?