zoukankan      html  css  js  c++  java
  • ObservableCollection删除问题

    做#Weather时,遇到个问题,需要从原来ObservableCollection中删除掉天气缓存

     类结构

    View Code
      1 public class WeatherInfoModel : INotifyPropertyChanged
      2     {
      3 
      4         #region private members
      5         private string _CityField;
      6 
      7         private string _DateField;
      8 
      9         private string _DayField;
     10 
     11         private string _NightField;
     12 
     13         private string _SensibleTemField;
     14 
     15         private string _dConditioinField;
     16 
     17         private string _dTemperatureField;
     18 
     19         private string _dWindPowerField;
     20 
     21         private string _nConditionField;
     22 
     23         private string _nTemperatureField;
     24 
     25         private string _nWindPowerField;
     26 
     27         private string _dImgPath;
     28 
     29         private string _nImgPathField;
     30 
     31         private string _temperatureField;
     32 
     33         #endregion
     34 
     35         #region public Properties
     36         public string City
     37         {
     38             get { return this._CityField; }
     39             set
     40             {
     41                 if (this._CityField != value)
     42                 {
     43                     this._CityField = value;
     44                     NotifyPropertyChanged("City");
     45                 }
     46             }
     47         }
     48 
     49         public string Date
     50         {
     51             get { return this._DateField; }
     52             set
     53             {
     54                 if (this._DateField != value)
     55                 {
     56                     this._DateField = value;
     57                     NotifyPropertyChanged("Date");
     58                 }
     59             }
     60         }
     61 
     62         public string Night
     63         {
     64             get { return _NightField; }
     65             set { _NightField = value; NotifyPropertyChanged("Night"); }
     66         }
     67 
     68         public string SensibleTem
     69         {
     70             get { return _SensibleTemField; }
     71             set { _SensibleTemField = value; NotifyPropertyChanged("SensibleTem"); }
     72         }
     73 
     74         public string DCondition
     75         {
     76             get { return _dConditioinField; }
     77             set { _dConditioinField = value; NotifyPropertyChanged("DConditioin"); }
     78         }
     79 
     80         public string Day
     81         {
     82             get { return _DayField; }
     83             set { _DayField = value; NotifyPropertyChanged("Day"); }
     84         }
     85 
     86         public string DTemperature
     87         {
     88             get { return _dTemperatureField; }
     89             set { _dTemperatureField = value; NotifyPropertyChanged("DTemperature"); }
     90         }
     91 
     92         public string DWindPower
     93         {
     94             get { return _dWindPowerField; }
     95             set { _dWindPowerField = value; NotifyPropertyChanged("DWindPower"); }
     96         }
     97 
     98         public string NCondition
     99         {
    100             get { return _nConditionField; }
    101             set { _nConditionField = value; NotifyPropertyChanged("NCondition"); }
    102         }
    103 
    104         public string NTemperature
    105         {
    106             get { return _nTemperatureField; }
    107             set { _nTemperatureField = value; NotifyPropertyChanged("NTemperature"); }
    108         }
    109 
    110         public string NWindPower
    111         {
    112             get { return _nWindPowerField; }
    113             set { _nWindPowerField = value; NotifyPropertyChanged("NWindPower"); }
    114         }
    115 
    116         public string DImgPath
    117         {
    118             get { return _dImgPath; }
    119             set { _dImgPath = value; NotifyPropertyChanged("DImaPath"); }
    120         }
    121 
    122         public string NImgPath
    123         {
    124             get { return _nImgPathField; }
    125             set { _nImgPathField = value; NotifyPropertyChanged("NImgPath"); }
    126         }
    127 
    128         public string Temperature
    129         {
    130             get { return _temperatureField; }
    131             set { _temperatureField = value; NotifyPropertyChanged("Temperature"); }
    132         }
    133 
    134 
    135         #endregion
    136 
    137         public event PropertyChangedEventHandler PropertyChanged;
    138 
    139         private void NotifyPropertyChanged(String propertyName)
    140         {
    141             PropertyChangedEventHandler handler = PropertyChanged;
    142             if (null != handler)
    143             {
    144                 handler(this, new PropertyChangedEventArgs(propertyName));
    145             }
    146         }

    部分代码

     1         #region 所有城市天气状况
     2         private ObservableCollection<Models.WeatherInfoModel> _allCityFiveDaysInfo = new ObservableCollection<Models.WeatherInfoModel>();
     3 
     4         public ObservableCollection<Models.WeatherInfoModel> AllCityFiveDaysInfo
     5         {
     6             get { return this._allCityFiveDaysInfo; }
     7             set { this._allCityFiveDaysInfo = value; }
     8         }
     9 
    10         #endregion

    出错代码

    错误现象,第一条数据删除后,第二次循环出错(理论上需要删除5条连续数据)。index变化??

    1  //foreach (Models.WeatherInfoModel cache in AllCityFiveDaysInfo)
    2 //{
    3 //    Models.WeatherInfoModel tem = cache;
    4 //    if (tem.City == city)
    5 //        AllCityFiveDaysInfo.Remove(tem);
    6 //}

    替换代码:

    1                 for (int i = 0; i < AllCityFiveDaysInfo.Count; i++)
    2                 {
    3                     if (AllCityFiveDaysInfo[i].City == city)
    4                     {
    5                         AllCityFiveDaysInfo.RemoveAt(i);
    6                         i--;
    7                     }
    8                 }
  • 相关阅读:
    [LeetCode]题解(python):041-First Missing Positive
    [LeetCode]题解(python):037-Sudoku Solver
    [LeetCode]题解(python):040-Combination Sum II
    [LeetCode]题解(python):039-Combination Sum
    [LeetCode]题解(python):038-Count and Say
    [LeetCode]题解(python):036-Valid Sudoku
    [LeetCode]题解(python):035-Search Insert Position
    [LeetCode]题解(python):034-Search for a Range
    [LeetCode]题解(python):033-Search in Rotated Sorted Array
    计算最长英语单词链
  • 原文地址:https://www.cnblogs.com/NailClipper/p/2700622.html
Copyright © 2011-2022 走看看