【问题标题】:Update/Refresh ListView or ListView Item without starting from the top更新/刷新 ListView 或 ListView 项目而不从顶部开始
【发布时间】:2019-04-24 01:31:34
【问题描述】:

有没有办法更新/刷新我的 ListView 或 ListView 项?目前更新/刷新我的 ListView 的唯一方法是:

public void NewsList_Selected(Object sender, SelectedItemChangedEventArgs e)
{
    var a = e.SelectedItem as NewsEntry;
    var b = from c in newsEntries
            where (a == c)
            select c;
    foreach(NewsEntry d in b)
    {
        d.Text = d.TextFull;
    }

    // Below is my update/refresh thing
    NewsList.ItemsSource = null;
    NewsList.ItemsSource = newsEntries;
 }

但这意味着如果我在我的 ListView 中向下滚动并选择一个项目,我将再次跳到我的 ListView 的顶部。但我需要留在我离开的地方。有解决办法吗?

【问题讨论】:

    标签: c# listview xamarin xamarin.forms selecteditem


    【解决方案1】:

    这样做的正确方法实际上是在您的模型类中使用INotifyPropertyChanged,并使用可观察的集合作为您的 ListView ItemsSource。

    • 首先,用INotifyPropertyChanged 继承你的类并实现它的属性,如下所示:

        public event PropertyChangedEventHandler PropertyChanged;  
      
        private void NotifyPropertyChanged(string propertyName)  
        {  
            if (PropertyChanged != null)  
            {  
               PropertyChanged(this, new PropertyChangedEventArgs(propertyName));  
            }  
         }  
      
    • 然后为您的 ListView 创建一个属性:

       private ObservableCollection<DataType> _FooCollection;
       public ObservableCollection<DataType> FooCollection { get{return _FooCollection; } set{_FooCollection = value; OnPropertyChanged(nameof(FooCollection )); }}
      
    • 在您的 Xaml 中将此集合指定为列表视图绑定:

       <ListView .... ItemsSource={Binding FooCollection} ..../>
      
    • 然后,当您必须更改列表视图数据时,您所要做的就是分配 FooCollection,它会自动为您完成剩下的工作。

    • 例如:

      public void NewsList_Selected(Object sender, SelectedItemChangedEventArgs e)
      {
           var a = e.SelectedItem as NewsEntry;
           var b = from c in newsEntries
             where (a == c)
             select c;
           foreach(NewsEntry d in b)
          {
            d.Text = d.TextFull;
          }
      
           FooCollection = newsEntries; // This will do the rest for you 
      }
      

    【讨论】:

      猜你喜欢
      • 2012-09-21
      • 2012-08-26
      • 1970-01-01
      • 2015-10-18
      • 1970-01-01
      • 1970-01-01
      • 2019-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多