添加新的 List 元素时触发的函数

Sor*_*scu 0 c# list count

我一直在寻找一种仅在添加 List 元素后才在代码中触发函数的方法,但我找不到任何信息。

我正在考虑用 的值创建另一个列表(参见代码)List.Count,但这就是我得到 15 个列表的方法......

有没有更好的办法?

谢谢

private List<int>       ListCountH;
...
ListCountH = new List<int>();
...
if (!LHsDmiAdd && b > 1 && HSwDMI[b-1] - HSwDMI[b] > 0.001 && HSwDMI[b-1] - HSwDMI[b-2] > 0.001)
{
  LastHSwDMI.Add(HSwDMI[b-1]);
  listCountLH = LastHSwDMI.Count;
  ListCountH.Add(listCountLH);   
  j++;
}
...
if (ListCountH[j-1] > ListCountH[j-2]) 
{
  // Logic triggered after ListCountH increase
}                          

Run Code Online (Sandbox Code Playgroud)

但这似乎不起作用:

var colL = new ObservableCollection<int>(ListCountL);
colL.CollectionChanged += (s, e) =>
{ 
   if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
      // This code never triggers, but in the previous code it did after 
      // if (ListCountH[j-1] > ListCountH[j-2])                         
      if (k > 1 && c > 1 && (lowSwBarDiffA <= BarDiff || lowSwBarDiffB <= BarDiff))
        {
          if (!posDiv && ((LastLSwDMI[k-1] - LastLSwDMI[k-2] > 0.001 && LastLSwDMIprice[k-2] - LastLSwDMIprice[k-1] > TickSize)   
        || (LSwDMI[c-1] - LastLSwDMI[k-1] > 0.001 && LastLSwDMIprice[k-1] - LSwDMIprice[c-1] > TickSize)))  
            posDiv = true;  
         else
            posDiv = false;
         }
};              
Run Code Online (Sandbox Code Playgroud)

当我尝试声明事件处理程序时,此方法也会引发很多编译错误:

public class Divergence5min : Strategy
{
   private MyList<int>      ListCountL, ListCountLastL;
   ...
  else if (State == State.Configure)                
  {
     ListCountL             = new MyList<int>();    
     ListCountLastL         = new MyList<int>();
  }

class MyList<T> : List<T> // Nested inside Divergence5min
{           
   public event Action<T> ItemAdded;            
   public new void Add(T obj)
   {                
     base.Add(obj);             
     if(ItemAdded != null)              
        ItemAdded.Invoke(obj);
   }
}

private void LookAfterLastLow()
{
    ListCountLastL.ItemAdded += AddItemEventHandler;
    void AddItemEventHandler()
    {   // The code I need executed after an item is added to ListCountLastL list
        if(listCountLL > 1)
        {       
            lastBarL = LastLSwDMIbar[k-1] + n;
            if (n <= 5)
            {
                newLastLowPrice = Lows[0].GetValueAt(lastBarL);
                if (newLastLowPrice < LastLSwDMIprice[k-1])
                {
                    LastLSwDMIprice[k-1] = newLastLowPrice;
                    newLastLowBar = lastBarL;
                    LastLSwDMIpriceBar[k-1] = newLastLowBar;
                    lowSwBarDiffB = Math.Abs(newLastLowBar - LastLSwDMIbar[k-1]);   
                    PrintLookAfterLow();                            
                }
                n++;
             }
        }               
    }
} 
Run Code Online (Sandbox Code Playgroud)

Mic*_*zyn 6

然后,您应该将“添加”逻辑封装在方法中:

public void AddElementToList(int element)
{
  ListCountH.Add(element);
  // your logic goes here
}
Run Code Online (Sandbox Code Playgroud)

或者您可以在类中定义一个Add事件,并在将元素添加到列表时引发它,该事件再次应该封装在单独的方法中:

public void AddElementToList(int element)
{
  ListCountH.Add(element);
  AddEvent?.Invoke();
}
Run Code Online (Sandbox Code Playgroud)

或者您可以编写类包装List来公开特殊Add方法并添加事件并使用该类而不是List.

编辑:你应该看看ObservableCollection:)

使用示例:

// Have to include this using
using System.Collections.ObjectModel;
Run Code Online (Sandbox Code Playgroud)

用法:

var ol = new ObservableCollection<int>();
ol.CollectionChanged += (s, e) =>
{
  if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
    Console.WriteLine("Item added!");
};
Run Code Online (Sandbox Code Playgroud)