自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

聊聊C# ObservableCollection和List

開發(fā) 后端
ObservableCollection這個類的方法,對數(shù)據(jù)的操作很少,重點放在了當(dāng)自己本事變化的時候(不管是屬性,還是集合)會調(diào)用發(fā)出通知的事件。

[[379495]]

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

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

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

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

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

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

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

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

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

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

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

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

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

二、舉例:

1、舉例1:

MainWindow.xaml:

  1. <ListBox x:Name="listbind" Height="61" HorizontalAlignment="Left" Margin="146,12,0,0" VerticalAlignment="Top" Width="120" >   
  2. <ListBox.ItemTemplate>   
  3. <DataTemplate>   
  4. <TextBlock Text="{Binding Name}" />   
  5. </DataTemplate>   
  6. </ListBox.ItemTemplate>   
  7. </ListBox>   
  8. <ListBox x:Name="observbind" Height="74" HorizontalAlignment="Left" Margin="146,111,0,0" VerticalAlignment="Top" Width="120" >   
  9. <ListBox.ItemTemplate>   
  10. <DataTemplate>   
  11. <TextBlock Text="{Binding Name}" />   
  12. </DataTemplate>   
  13. </ListBox.ItemTemplate>   
  14. </ListBox>   
  15. <TextBlock Height="23" HorizontalAlignment="Left" Margin="38,58,0,0" Name="textBlock1" Text="List綁定數(shù)據(jù)" VerticalAlignment="Top" />   
  16. <TextBlock Height="44" HorizontalAlignment="Left" Margin="12,125,0,0" Name="textBlock2" Text="ObservableCollection綁定數(shù)據(jù)" VerticalAlignment="Top" Width="112" />   
  17. <Button Content="Button" Height="23" HorizontalAlignment="Left"  

 

 

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

Person.cs:

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

MainWindow.xaml.cs:

  1. private List<Person> person1 = new List<Person>(); 
  2. private ObservableCollection<Person> person2 = new ObservableCollection<Person>(); 
  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)行程序點擊button按鈕,然后只有ObservableCollection的有添加。 

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

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

2、舉例2

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

  1. private ObservableCollection<PreviewListModel> _previewList = new ObservableCollection<PreviewListModel>(); 
  2. /// <summary> 
  3. /// 預(yù)覽信息列表 
  4. /// </summary> 
  5. public ObservableCollection<PreviewListModel> 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<PatientInfo_Table> 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<PatientStudy_Table> patientStudyList = dataBase.PatientStudy_Table.Where(u => u.PatientKey < 10).ToList(); 
  13. //按條件檢索集合 
  14. List<PatientInfoHistroryModel> 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("病人歷史記錄信息表【高級查詢】狀態(tài)下,發(fā)生數(shù)據(jù)庫錯誤。錯誤信息:--------------" + ex.ToString()); 
  43. LogHelper.Error("OperateDataSheetViewModel.cs::AdvancedSearchFunc()高級查詢失敗--" + ex.Message); 

四、總結(jié)

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

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

參考連接:

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

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

 

責(zé)任編輯:武曉燕 來源: CSharp編程大全
相關(guān)推薦

2024-09-29 09:28:38

Action?C#

2023-10-09 07:11:03

排序算法序列

2024-11-28 09:57:50

C#事件發(fā)布器

2024-05-15 09:11:51

委托事件C#

2024-10-21 16:59:37

C#編程多線程

2009-08-13 17:04:09

C#語言C#程序

2009-08-27 16:11:03

C# delegateC# event

2009-08-26 10:34:15

C#類型C#變量

2024-09-18 08:00:05

C#編程

2009-08-19 16:50:32

Visual C#C#語言特性

2023-10-10 08:00:07

2023-09-01 10:00:17

2023-09-11 08:20:17

對象閉包底層

2025-02-13 09:32:12

C#重寫override

2024-09-09 11:26:36

C#數(shù)字格式化

2009-08-10 10:04:25

C#抽象類C#接口

2009-09-01 17:51:47

C#拆箱C#裝箱

2009-08-19 10:09:21

C#和C++

2024-08-26 08:34:47

AES加密算法

2024-07-10 08:31:59

C#特性代碼
點贊
收藏

51CTO技術(shù)棧公眾號