简体   繁体   English

如何在Collection给出项目数时调用方法

[英]How to call method when Collection has given count of items

I would like to invoke a method when into my List property were added specific count of items. 我想在我的List属性中添加特定的项目数时调用一个方法。 Actually I need to find way how to automatically check if a Collection has given count of items and after that call a method. 实际上我需要找到方法如何自动检查一个Collection是否已经给出了项目数量,然后调用了一个方法。

Ok, one example. 好的,一个例子。 I have a class MyClass 我有一个MyClass类

public class MyClass
{
    public List<string> Url { get; set; }

    public void fullList()
    {
        //some work with the List
    }
}

And now somewhere in main function I create instance of MyClass and add some items. 现在在main函数的某个地方我创建了MyClass的实例并添加了一些项目。

MyClass mc = new MyClass();
mc.Url = new List<string>();
mc.Url.Add("www.1.com");
mc.Url.Add("www.2.com");
//now on 3rd added item I want to invoke method fullList from class MyClass
mc.Url.Add("www.3.com");

You should probably create have a class that encapsulates the URL list and the behavior you want. 您应该创建一个包含URL列表和所需行为的类。 I guess the behavior is that the list gets "full" when it hits 3 items and then you want an event to be raised that MyClass can listen on. 我猜行为是当列表遇到3个项目然后你想要一个MyClass可以监听的事件时,列表会变得“满”。

class MyUrls 
{
    List<string> _urls = new List<string>();

    public void Add(string url)
    {
        _urls.Add(url);
        if (_urls.Count == 3 && OnFull != null)
            OnFull.Invoke();
    }

    public IEnumerable<string> Urls
    {
        get
        {
            return _urls;
        }
    }

    public event Action OnFull;
}

and use it like this: 并像这样使用它:

public class MyClass
{
    public MyClass()
    {
       Urls.OnFull += fullList;
    }
    public MyUrls Urls { get; }

    public void fullList()
    {
        //some work with the List
    }
}

MyClass mc = new MyClass();
mc.Urls = new List<string>();
mc.Urls.Add("www.1.com");
mc.Urls.Add("www.2.com");
//now on 3rd added item I want to invoke method fullList from class MyClass
mc.Urls.Add("www.3.com");

Don't reinvent the wheel - use the ObservableCollection<T> . 不要重新发明轮子 - 使用ObservableCollection<T>

You can attach a handler to the CollectionChanged event that gets called when the collection is modified. 您可以将处理程序附加到在修改集合时调用的CollectionChanged事件。

ObservableCollection<string> collection = new ObservableCollection<string>();

collection.CollectionChanged += (sender, args) => {
    if(args.Action == NotifyCollectionChangedAction.Add &&
        collection.Count == 3)
        HandleItemsAdded(args.NewItems.Cast<string>());         
};

collection.Add("www.1.com");
collection.Add("www.2.com");
collection.Add("www.3.com");

Handler: 处理器:

public static void HandleItemsAdded(IEnumerable<string> newItems)
{
    foreach(var item in newItems)
        Console.WriteLine(item);
}

Working Fiddle: http://dotnetfiddle.net/eIeilP 工作小提琴: http//dotnetfiddle.net/eIeilP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM