从事件的“发件人”获取信息的更好的形式是什么?要转换为已知发件人的类型,还是直接使用该名称?我怀疑铸造是更好的形式,但如果你知道这个名字的话,这似乎是一种相当迂回的方式。以下是我正在考虑的例子:
protected void OnEntry1Changed (object sender, System.EventArgs e) {
if ((sender as Gtk.Entry).Text != "some specific value" && (sender as Gtk.Entry).Text != "" && anotherEntry.Text !="") {
// Do some stuff here...
} else {
// Do some other stuff here...
}
}
另一个版本是直接访问该条目:
protected void OnEntry1Changed(object sender, System.EventArgs e) {
if (Entry1.Text != "some specific value" && Entry1.Text != "" && anotherEntry.Text !="") {
//Do some stuff here...
} else {
// Do some other stuff here...
}
}
......似乎第二个版本更直接,但是其中一个被认为是更“正确的代码形式”?
答案 0 :(得分:1)
我更喜欢做最好的一个 - (sender as Gtk.Entry)
。这样,如果您必须在应用程序的其他位置复制该条目的功能,则可以重用信号处理程序。