【问题标题】:Update or refresh Listview in Windows Phone 8.1 app with C# and XAML使用 C# 和 XAML 在 Windows Phone 8.1 应用程序中更新或刷新 Listview
【发布时间】:2015-06-10 17:24:17
【问题描述】:

ItemsSource 更新时,我需要刷新ListView。我已在 XAML 文件中将 myListView 声明为 ListView,并将 myList 分配为 C# 代码中的 itemssource,并使用以下代码 sn-p:

myListView.ItemsSource = myList;

现在,我的问题是,如何刷新myListView

【问题讨论】:

  • 你应该使用 MVVM。使这些东西变得微不足道。去学习吧。

标签: c# xaml listview windows-phone-8


【解决方案1】:

你说的清爽是什么意思?如果你希望你的 UI 被刷新,你的 myList 必须是 ObservableCollection 类型并且你的类必须实现 INotifyPropertyChanged 接口。

看看这篇文章

https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged%28v=vs.110%29.aspx

【讨论】:

    【解决方案2】:

    你需要使用和 ObservableCollection

    public ObservableCollection<string> MyList { get; set; }
    

    将可观察集合设置为您的 ItemsSource 后,对集合的每次更改都会自动反映在您的列表视图中。

    【讨论】:

      【解决方案3】:

      很高兴看到它只能通过使用System.Collections.ObjectModel.ObservableCollection 起作用,然后将集合设置在ListView.ItemsSource 中。

      【讨论】:

        【解决方案4】:

        试试这个

          if (ListView.Items.Count > 0)
          {
             ListView.ItemsSource = null;
          }
          listItem.Clear();
        

        仅分配新的列表视图对象不会清除数据。 您需要清除 ItemSource 并清除用于绑定列表视图的列表数组

        【讨论】:

          【解决方案5】:

          我创建了一个测试应用程序 - krayknot 来测试这个场景,这里是代码:

          包括 INotifyPropertyChanged

          public sealed partial class PivotPage : Page, INotifyPropertyChanged
          

          创建事件处理程序

          public event PropertyChangedEventHandler PropertyChanged;
          

          创建 ObservableCollection

          ObservableCollection<Feeds> oc = new ObservableCollection<Feeds>();
          

          这是方法,

          private async void BindFeeds(string URL)
                  {
                      progressRing.IsActive = true;
          
                      var uri = new Uri(URL);
                      var httpClient = new HttpClient();
          
                      // Always catch network exceptions for async methods
                      httpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
                      var result = await httpClient.GetAsync(uri);
          
                      var xmlStream = await result.Content.ReadAsStreamAsync();
                      XDocument loadedData = XDocument.Load(xmlStream);
          
                      foreach (var data in loadedData.Descendants("Table"))
                      {
          
          
                          try
                          {
                              oc.Add(new Feeds
                              {
                                  BlogId = data.Element("Blogs_CID").Value.ToString(),
                                  Description = data.Element("Blogs_BriefDescription").Value.ToString(),
                                  IconImage = videoIcon,
                                  Heading = data.Element("Blogs_Heading").Value.ToString().Trim(),
                                  MainImage = feedImageThumbnail, //data.Element("Blogs_SquareImagePath").Value.ToString(),
                                  TimeStamp = data.Element("TimeElapsed").Value.ToString(),
                                  SourceURL = data.Element("Blogs_SourceURL").Value.ToString()
                              });
          
                          }
                          catch (Exception)
                          {
                              //throw;
                          }               
                      }
                      httpClient.Dispose();
          
                      if (lstLatest.Items.Count == 0)
                      {
                          lstLatest.ItemsSource = oc;
                      }
                      else
                      {
                          NotifyPropertyChanged();
                      }
          
                      httpClient.Dispose();
                      progressRing.IsActive = false;
                  }
          

          【讨论】:

            猜你喜欢
            • 2014-10-21
            • 1970-01-01
            • 2012-08-14
            • 1970-01-01
            • 1970-01-01
            • 2015-06-20
            • 2016-11-21
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多