日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
聊聊C#ObservableCollection和List

一、ObservableCollection和List的區(qū)別

1)ObservableCollection比較簡單,繼承了Collection, INotifyCollectionChanged, INotifyPropertyChanged

Collection:為泛型集合提供基類。

INotifyCollectionChanged:將集合的動(dòng)態(tài)更改通知給偵聽器,例如,何時(shí)添加和移除項(xiàng)或者重置整個(gè)集合對象。

INotifyPropertyChanged:向客戶端發(fā)出某一屬性值已更改的通知。

所以再ObservableCollection這個(gè)類的方法,對數(shù)據(jù)的操作很少,重點(diǎn)放在了當(dāng)自己本事變化的時(shí)候(不管是屬性,還是集合)會(huì)調(diào)用發(fā)出通知的事件。(一般用于更新UI,當(dāng)然也可以用于寫其他的事情。這個(gè)以后會(huì)寫)

2)List就比較多了,繼承了IList, ICollection, IEnumerable, IList, ICollection, IEnumerable。

IList:表示可按照索引單獨(dú)訪問的一組對象。

ICollection:定義操作泛型集合的方法。

IEnumerable:公開枚舉器,該枚舉器支持在指定類型的集合上進(jìn)行簡單迭代。

IList:表示可按照索引單獨(dú)訪問的對象的非泛型集合。

ICollection:定義所有非泛型集合的大小、枚舉器和同步方法。

IEnumerable:公開枚舉器,該枚舉器支持在非泛型集合上進(jìn)行簡單迭代。

二、舉例:

1、舉例1:

MainWindow.xaml:

 
 
 
 
  1.    
  2.    
  3.    
  4.    
  5.    
  6.    
  7.    
  8.    
  9.    
  10.    
  11.    
  12.    
  13.    
  14.    
  15.    
  16.    

xaml頁面很簡單,托2個(gè)listbox分別用來綁定ObservableCollection和List

Person.cs:

 
 
 
 
  1. public class Person   
  2.      { 
  3.          public string Name { get; set; } 
  4.      } 

MainWindow.xaml.cs:

 
 
 
 
  1. private List person1 = new List(); 
  2. private ObservableCollection person2 = new ObservableCollection(); 
  3. public DemoTestDiff()   
  4. InitializeComponent(); 
  5. person1.Add(new Person() { Name = "張三" }); 
  6. person1.Add(new Person() { Name = "李四" }); 
  7. listbind.ItemsSource = person1; 
  8. person2.Add(new Person() { Name = "張三" }); 
  9. person2.Add(new Person() { Name = "李四" }); 
  10. observbind.ItemsSource = person2; 
  11. private void button1_Click(object sender, RoutedEventArgs e)   
  12. person1.Add(new Person() { Name = "王五" }); 
  13. person2.Add(new Person() { Name = "王五" }); 
 
 
 
 
  1. 運(yùn)行程序點(diǎn)擊button按鈕,然后只有ObservableCollection的有添加。 

表示當(dāng)集合對象的集合改變時(shí),只有ObservableCollection會(huì)發(fā)出通知更新UI。

這只是他們兩個(gè)區(qū)別之一。

2、舉例2

以下方法可以更新ListView的UI:

 
 
 
 
  1. private ObservableCollection _previewList = new ObservableCollection(); 
  2. ///  
  3. /// 預(yù)覽信息列表 
  4. ///  
  5. public ObservableCollection PreviewList 
  6. get { return _previewList; } 
  7. set { SetProperty(ref _previewList, value); } 
  8. //set { _previewList = value; RaisePropertyChanged("PreviewList"); } 

三、 ObservableCollection和List的互相轉(zhuǎn)換

https://www.cnblogs.com/warioland/archive/2011/11/08/2240858.html

從數(shù)據(jù)庫檢索的出來的集合是List 類型,我們需要把它轉(zhuǎn)成ObservableCollection類型怎么辦?如下方法:

 
 
 
 
  1. T tList = new List(tObjectStruct .ToList()); 
  2. ObservableCollection tObjectStruct  = new  

數(shù)據(jù)庫檢索:

 
 
 
 
  1. public void AdvancedSearchFunc(AdvancedSearchNotification advancedSearchNotification) 
  2. try 
  3. KrayMobileDREntities dataBase = new KrayMobileDREntities(); 
  4. //每次使用前必須清零 
  5. patientInfoHistroryModel.Clear(); 
  6. //先把數(shù)據(jù)庫的數(shù)據(jù)提取出來,放到集合中。 
  7. List patientInfoList = 
  8. dataBase.PatientInfo_Table.Where(u => u.PatientKey.ToString().Equals(advancedSearchNotification.PatientInfo) 
  9. || u.PatientID.ToString().Equals(advancedSearchNotification.StudyID) 
  10. || u.PatientName.ToString().Equals(advancedSearchNotification.PatientName) 
  11. ).ToList(); 
  12. List patientStudyList = dataBase.PatientStudy_Table.Where(u => u.PatientKey < 10).ToList(); 
  13. //按條件檢索集合 
  14. List list = 
  15. (from pI in patientInfoList 
  16. where (pI.PatientKey < 1000) 
  17. select new PatientInfoHistroryModel() 
  18. PatientInfo = pI.PatientKey.ToString(), 
  19. StudyID = pI.PatientID.ToString(), 
  20. PatientName = pI.PatientName.ToString(), 
  21. PatientSex = pI.PatientSex.ToString(), 
  22. PatientAge = pI.PatientAge.ToString(), 
  23. PatientBrith = pI.PatientBirthDate.ToString(), 
  24. PatientHeight = pI.PatientHeight.ToString(), 
  25. PatientWeight = pI.PatientWeight.ToString(), 
  26. RecordSource = pI.PatientSource.ToString(), 
  27. //StudyTime       = PS.StudyDatetime, 
  28. //EquipmentType   = PS.StudyPhysician, 
  29. //StudyPart       = PS.StudyType, 
  30. //SequenceAmount  = PS.SeriesCount, 
  31. StudyTime = pI.PatientAge.ToString(), 
  32. EquipmentType = pI.PatientAge.ToString(), 
  33. StudyPart = pI.HangFlag.ToString(), 
  34. SequenceAmount = pI.HangFlag.ToString(), 
  35. StudyStutas = pI.StudyCompleteFlag.ToString(), 
  36. SuspendState = pI.HangFlag.ToString(), 
  37. FilmPrint = pI.PrintFlag.ToString(), 
  38. }).ToList(); 
  39. patientInfoHistroryModel = list; 
  40. dataBase.Dispose(); 
  41. catch (Exception ex) 
  42. MessageBox.Show("病人歷史記錄信息表【高級(jí)查詢】狀態(tài)下,發(fā)生數(shù)據(jù)庫錯(cuò)誤。錯(cuò)誤信息:--------------" + ex.ToString()); 
  43. LogHelper.Error("OperateDataSheetViewModel.cs::AdvancedSearchFunc()高級(jí)查詢失敗--" + ex.Message); 

四、總結(jié)

1、ObservableCollection表示一個(gè)動(dòng)態(tài)數(shù)據(jù)集合,在添加項(xiàng)、移除項(xiàng)或刷新整個(gè)列表時(shí),此集合將提供通知。

2、List表示可通過索引訪問的對象的強(qiáng)類型列表。提供用于對列表進(jìn)行搜索、排序和操作的方法。(大部分操作用Linq,很強(qiáng)大也很方便。

參考連接:

https://blog.csdn.net/xpj8888/article/details/84782949

本文轉(zhuǎn)載自微信公眾號(hào)「CSharp編程大全」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系CSharp編程大全公眾號(hào)。   


分享題目:聊聊C#ObservableCollection和List
當(dāng)前地址:http://www.dlmjj.cn/article/dpjcjis.html