C#-简单类的事件
我很难理解事件是如何在C#上工作的到目前为止,我只在控制台应用程序中进行测试。我有时会根据我在MSDN文档中看到的内容进行尝试,但没有成功 这是我的代码:C#-简单类的事件,c#,.net,events,delegates,C#,.net,Events,Delegates,我很难理解事件是如何在C#上工作的到目前为止,我只在控制台应用程序中进行测试。我有时会根据我在MSDN文档中看到的内容进行尝试,但没有成功 这是我的代码: using System; using System.Collections.Generic; namespace Events { class MainClass { public static void Main(string[] args) { TodoList
using System;
using System.Collections.Generic;
namespace Events
{
class MainClass
{
public static void Main(string[] args)
{
TodoList list = new TodoList();
TodoItem fooItem = new TodoItem
{
Title = "FooItemTitle",
Description = "FooItemDescription",
};
TodoItem barItem = new TodoItem
{
Title = "BarItemTitle",
Description = "BarItemDescription",
};
// I want to trigger an event everytime a item is added on the
// TodoList.
// How can I do that?
list.AddItem(fooItem);
list.AddItem(barItem);
}
}
class TodoList
{
List<TodoItem> items;
public TodoList()
{
this.items = new List<TodoItem>();
}
public void AddItem(TodoItem item)
{
this.items.Add(item);
}
}
class TodoItem
{
public String Description;
public String Title;
public override string ToString()
{
return string.Format("[TodoItem]: Title={0} | Description={1}", this.Title, this.Description);
}
}
}
使用系统;
使用System.Collections.Generic;
命名空间事件
{
类主类
{
公共静态void Main(字符串[]args)
{
TodoList list=新TodoList();
TodoItem fooItem=新TodoItem
{
Title=“FooItemTitle”,
Description=“FooItemDescription”,
};
TodoItem barItem=新TodoItem
{
Title=“BarItemTitle”,
Description=“baritemsdescription”,
};
//我想在每次向上添加项目时触发一个事件
//托多利斯特。
//我该怎么做?
清单.附件(项目);
列表.附加项(barItem);
}
}
托多利斯特阶级
{
清单项目;
公共托多利斯特()
{
this.items=新列表();
}
公共无效附加项(TodoItem项)
{
此.items.Add(item);
}
}
类TodoItem
{
公共字符串描述;
公共字符串标题;
公共重写字符串ToString()
{
返回string.Format(“[TodoItem]:Title={0}| Description={1}”,this.Title,this.Description);
}
}
}
如何配置每次在
TodoList
上添加TodoItem
时触发的事件?您可以将事件添加到TodoList
:
// note how we assigned a blank delegate to it
// this is to avoid NullReferenceException when we fire event with no subscribers
public event EventHandler<TodoItem> ItemAdded = (s, e) => { };
另外,不要忘记订阅其他类的事件:
// as eventhandler you should use method that accepts exactly the same
// number and type of parameters as in delegate EventHandler<TodoItem>
list.ItemAdded += (eventhandler);
//作为eventhandler,您应该使用接受完全相同的
//委托EventHandler中的参数数量和类型
list.ItemAdded+=(eventhandler);
您可以将事件添加到ToDoList
:
// note how we assigned a blank delegate to it
// this is to avoid NullReferenceException when we fire event with no subscribers
public event EventHandler<TodoItem> ItemAdded = (s, e) => { };
另外,不要忘记订阅其他类的事件:
// as eventhandler you should use method that accepts exactly the same
// number and type of parameters as in delegate EventHandler<TodoItem>
list.ItemAdded += (eventhandler);
//作为eventhandler,您应该使用接受完全相同的
//委托EventHandler中的参数数量和类型
list.ItemAdded+=(eventhandler);
您可以使用可观察集合
而不是列表
,这将为您创建事件Hanks Eric,我来看看。但我的目的是了解事件流程。您可以使用可观察收集
而不是列表
,这将为您创建事件。Hanks Eric,我来看看。但我的目的是理解事件流。结果表明,我的错误是忘记在我的AddItem
方法中调用事件。。。谢谢。原来我的错误是忘记调用我的AddItem
方法上的事件。。。非常感谢。