为什么我的事件总是空的?

时间:2013-12-30 00:02:48

标签: c# windows-phone-8

我正在尝试使用事件提升的代码。但每当事件发生时,if语句永远不会成立,因为PropertyChanged始终为空。

 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);
      }
 }

这就是我订阅此活动的方式:

data.facebook.PropertyChanged+=new Facebook.MyEventHandler(Facebook_PropertyChanged);
data.PropertyChanged+= new RootObject.MyEventHandler( data_PropertyChanged);

这是班级

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);
    }
}

}

这是初始化数据的方式

RootObject data = new RootObject();

可能的原因是什么?

更新

好的,我想出来了。将值分配给数据时,会创建一个临时Facebook对象。然后将其复制到data.Facebook(复制构造函数)。这就是为什么没有订阅临时Facebook对象并在事件中给出null。因为它是使用复制构造函数复制的。在data.facebook中没有触发NotifyPropertyChanged。现在我把它缩小了。但我不知道这是什么工作。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

仅当您将事件处理程序添加到类的另一个实例时,才可以执行此操作。检查是否有其他情况?

另外,为什么不使用内置的INotifyPropertyChanged界面并为此创建自己的委托呢?

修改

正如我在您的代码中看到的,您创建了Facebook类的实例:

Facebook facebook = new Facebook();

但是您没有订阅实例的PropertyChanged事件。

此外,您允许其他开发人员在此处将Facebook类的实例替换为另一个:

public Facebook Facebook { 
    get 
    { 
        return facebook; 
    }
    set 
    {
        facebook = value;
    }
}  

所以有可能在这里存储另一个未初始化的对象。我建议删除公共集。

最后:我没看到这两个类如何相互连接。我只看到Facebook中的RootObject课程即时更新。