小编Sor*_*scu的帖子

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

我一直在寻找一种仅在添加 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, …
Run Code Online (Sandbox Code Playgroud)

c# list count

0
推荐指数
1
解决办法
556
查看次数

标签 统计

c# ×1

count ×1

list ×1