1
我對C#和GUI事件總的來說很新鮮。C#事件和繼承
我有一個基類:
public partial class BaseClass : UserControl
{
private event DataChangeEventHandler mDataChangeEvent;
public event DataChangeEventHandler DataChange
{
add
{
mDataChangeEvent += value;
}
remove
{
mDataChangeEvent -= value;
}
}
}
public delegate void DataChangeEventHandler(object sender, EventArgs e);
...
...
protected virtual void Control1_SelectedValueChanged(object sender, EventArgs e)
{
//some code do something
if(mDataChangeEvent != null)
{
EventArgs e = new EventArgs();
mDataChangeEvent(this, e);
}
}
}
// derived class
public partial class DerivedClass : BaseClass
{
...
...
protected override void Control1_SelectedValueChanged(object sender, EventArgs e)
{
base.Control1_SelectedValueChanged(sender, e);
}
}
我跑我的代碼通過調試。每當我將下拉控件更改爲另一個值時,它將觸發我的派生類'Control1_SelectedValueChanged'的中斷點,然後調用基類'Control1_SelectedValueChanged'。但是,mDataChangeEvent爲null。爲什麼?
這意味着沒有人正在收聽該事件。您錯過了訂閱活動? –
嗨Sriram,首先謝謝你。這就說得通了。我的派生類是否訂閱了該事件或類CallingClass,它使用我的派生類,它是否由AnotherClass實例化,或者我的AnotherClass是否訂閱了該事件?我希望有道理 – Allen