首页 > 代码库 > C# 可观察集合

C# 可观察集合

 1 static void Main()
 2     {
 3       var data = http://www.mamicode.com/new ObservableCollection<string>();
 4       data.CollectionChanged += Data_CollectionChanged;
 5       data.Add("One");
 6       data.Add("Two");
 7       data.Insert(1, "Three");
 8       data.Remove("One");
 9 
10     }
11 
12     static void Data_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
13     {
14       Console.WriteLine("action: {0}", e.Action.ToString());
15 
16       if (e.OldItems != null)
17       {
18         Console.WriteLine("starting index for old item(s): {0}", e.OldStartingIndex);
19         Console.WriteLine("old item(s):");
20         foreach (var item in e.OldItems)
21         {
22           Console.WriteLine(item);
23         }
24       }
25       if (e.NewItems != null)
26       {
27         Console.WriteLine("starting index for new item(s): {0}", e.NewStartingIndex);
28         Console.WriteLine("new item(s): ");
29         foreach (var item in e.NewItems)
30         {
31           Console.WriteLine(item);
32         }
33       }
34 
35 
36       Console.WriteLine();
37 
38     }

 

C# 可观察集合