如何在c#中使用带有集合的事件处理程序 [英] how to use event handlers with collections in c#
问题描述
您好,
我想检测队列中是否添加了某些项目或者列表。在添加该项目时,我必须弹出一条消息。我尝试过很多东西,但是工作很有用。
简单来说,如何使用集合处理事件处理。
谢谢和问候
你看过 ObservableCollection [ ^ ]?它实现INotifyPropertyChanged
和INotifyCollectionChanged
开箱即用。看起来这正是您正在寻找的。 blockquote>
首先检查: List vs ObservableCollection vs Silverlight中的INotifyPropertyChanged [ ^ ]
解决此问题的最佳方法是使用.NET的'ObservableCollection及其INotify___工具。
有些人真的很难掌握这个,我建议你通过研究.NET中定义自定义事件的标准方法来做好准备:[ ^ ]。
这是一个继承自List< int>的类的示例...它是一个通用的整数集合...它实现了一个自定义事件:
使用系统;
使用 System.Collections.Generic;
命名空间 Jan_1_2014_CollectionWithNotification
{
public class CustomIntCollection:List< int>
{
// 我们要公开整数的值
// 当前正被添加到集合中
// 需要自定义EventArgs类
// 此处此类嵌套在集合中
// ......可以在集合之外定义
public class NewIntAddEventArgs:EventArgs
{
public int intValue {获得跨度>; set ; }
public NewIntAddEventArgs( int theInt){intValue = theInt; }
}
// 这是可用的EventHandler方法
// 由此类消费者订阅
public event EventHandler< NewIntAddEventArgs> NewIntAdded;
public void 添加( int newInt)
{
// 调用自定义事件
OnRaiseNewIntAdded( new NewIntAddEventArgs(newInt));
// 让基本内部集合处理'添加
base .Add(newInt);
}
// 遵循标准MS C#的自定义事件代码
// 提升自定义事件的标准
protected virtual void OnRaiseNewIntAdded(NewIntAddEventArgs e)
{
EventHandler< NewIntAddEventArgs> handler = NewIntAdded;
// 仅在有订阅者的情况下触发事件!
if (handler!= null )handler( this ,e);
}
}
}您可以像这样测试此类的通知事件工具:
// 在某个表单中
CustomIntCollection cIntCollection = new CustomIntCollection( );
private void SomeForm_Load(对象 sender,EventArgs e)
{
// 将EventHandler绑定到此实例
cIntCollection.NewIntAdded + = cIntCollection_NewIntAdded;
}
// 现在绑定到CustomIntCollection实例的EventHandler:
private void cIntCollection_NewIntAdded( object sender,CustomIntCollection.NewIntAddEventArgs e)
{
Console.WriteLine( the integer added = + e.intValue.ToString());
}
// 代码中方法内的某处
// 测试为实例添加int和int的范围:
cIntCollection.Add( 100 );
cIntCollection.AddRange( new List< int> { , 2 , 3 , 4 });创建遵循MS准则的自定义事件的代码(是的,您可以通过其他方式完成)并使用它们,对于C#的新生来说,通常看起来很奇怪,但是你会习惯它,我相信,你会发现,似乎很多额外的工作与允许:
1.代码中的安全:防止 - 效果
2.创建在继承类中可以覆盖的结构/方法。
[edit]编辑v3 [Nelek]第一个代码块从xml更改为c#以改善颜色格式和可读性[/ edit]
[edit]编辑v4 [Nelek]编辑v3和海报请求中此行的更改说明[/ edit] 小>
Hello,
I want to detect if some item was added in a queue let us say or a list. On the addition of that item i must get a message pop up. I have tried many things but dint work.
In simple words how to use event handling with collections.
Thanks and regards解决方案Have you had a look at ObservableCollection[^] yet? It implementsINotifyPropertyChanged
andINotifyCollectionChanged
out of the box. Looks like that's exactly what you are looking for.
Check this first: List vs ObservableCollection vs INotifyPropertyChanged in Silverlight[^]
The best way to approach this is to use .NET's 'ObservableCollection, and its INotify___ facilities.
Some people really struggle with mastering this, and I recommend you prepare yourself by studying the standard way custom events are defined in .NET: [^].
Here's an example of a Class that inherits from List<int> ... it's a generic collection of integers ... which implements a custom event:
using System; using System.Collections.Generic; namespace Jan_1_2014_CollectionWithNotification { public class CustomIntCollection : List<int> { // we want to expose the value of the integer // that is currently being added to the collection // that requires a custom EventArgs class // here this class is nested within the collection // ... it could be defined outside the collection public class NewIntAddEventArgs : EventArgs { public int intValue { get; set; } public NewIntAddEventArgs(int theInt) { intValue = theInt; } } // this is the EventHandler method that will be available // to "subscribe" to by consumers of this class public event EventHandler<NewIntAddEventArgs> NewIntAdded; public void Add(int newInt) { // invoke the custom Event OnRaiseNewIntAdded(new NewIntAddEventArgs(newInt)); // let the base internal collection handle the 'Add base.Add(newInt); } // the custom Event code following standard MS C# // standards for raising a custom event protected virtual void OnRaiseNewIntAdded(NewIntAddEventArgs e) { EventHandler<NewIntAddEventArgs> handler = NewIntAdded; // fire the Event only if there are subscribers ! if (handler != null) handler(this, e); } } }You might test the notification event facility of this class like this:
//in some Form CustomIntCollection cIntCollection = new CustomIntCollection(); private void SomeForm_Load(object sender, EventArgs e) { // bind an EventHandler to this instance cIntCollection.NewIntAdded += cIntCollection_NewIntAdded; } // the EventHandler now bound to the instance of CustomIntCollection: private void cIntCollection_NewIntAdded(object sender, CustomIntCollection.NewIntAddEventArgs e) { Console.WriteLine("the integer added = " + e.intValue.ToString()); } // somewhere inside a method in your code // test adding an int, and range of ints to the instance: cIntCollection.Add(100); cIntCollection.AddRange(new List<int>{1,2,3,4});The code to create custom Events that follows the MS Guidelines (yes, you could do it other ways), and use them, often seems quite strange to new students of C#, but you'll get used to it, and, I believe, you'll realize that what might appear to be a lot of "extra" work is related to allowing:
1. "safety" in code: preventing side-effects
2. creating structures/methods that can be over-ridden in inheriting classes.
[edit]Edit v3 [Nelek] First code block changed from xml to c# to improve color format and readability[/edit]
[edit]Edit v4 [Nelek] Explanation of changes in edit v3 and this line in request of poster[/edit]
这篇关于如何在c#中使用带有集合的事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!