我有两堂课。在一堂课中,我创建并引发了一个事件,如下所示:
CustomerAdd类
public class CustomerAdd
{
public delegate void Done(object Sender, EventArgs e);
public event Done ListUpdated;
public void UpdateNewList()
{
//adding items to a generic List<T>,code removed as not relevant to post
//and raising the event afterwards
if (ListUpdated != null)
{
ListUpdated(this, EventArgs.Empty);
}
}
}
MyWindow类
public class MyWindow
{
private void SaveToDisk()
{
CustomerAdd cuss = new CustomerAdd();
cuss.ListUpdated += new CustomerAdd.Done(DisplayDetails);
cuss.UpdateNewList();
}
private void DisplayDetails()
{
//other codes here
}
}
现在,当我从SaveToDisk
类调用MyWIndow
方法时(由于我正在向DisplayDetails
事件订阅ListUpDated
方法),DisplayDetails
为不叫。调试器显示ListUpdated
为空。我已经搜索了几个小时,但未能提出解决方案。我关注了this link,但ListUpdated
仍然为空。任何指导/帮助将不胜感激。
答案 0 :(得分:1)
尝试一下:
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
CustomerReceive cr = new CustomerReceive();
cr.SaveToDisk();
}
}
public class CustomerAdd
{
public delegate void Done(object Sender, EventArgs e);
public event Done ListUpdated;
public void UpdateNewList()
{
//adding items to a generic List<T>,code removed as not relevant to post
//and raising the event afterwards
if (ListUpdated != null)
{
ListUpdated.Invoke(this, EventArgs.Empty);
}
}
}
public class CustomerReceive
{
public void SaveToDisk()
{
CustomerAdd cuss = new CustomerAdd();
cuss.ListUpdated += new CustomerAdd.Done(DisplayDetails);
cuss.UpdateNewList();
}
private void DisplayDetails(object Sender, EventArgs e)
{
int k = 0;
}
}
}
您需要对委托和事件进行良好的阅读,因为当有更多的侦听器时,这将不起作用
答案 1 :(得分:1)
有效:
$("#contractor").change(function() {
$(this).val() === 'addnewcon'
$('#addnewcon').modal({
show: true
});
});