1

我的列表框没有更新/刷新时遇到问题。我在这里读过我需要使用ObservableCollection但我没有任何运气。我正在从 XML 填充我的列表框。

 public class PestotoraPost
    {
        public string ID { get; set; }
        public string Date { get; set; }
        public string Name { get; set; }
        public string Message { get; set; }

    }
void WebLoad()
    {
        WebClient pestotora = new WebClient();
        pestotora.DownloadStringCompleted += new DownloadStringCompletedEventHandler(pestotora_DownloadStringCompleted);
        pestotora.DownloadStringAsync(new Uri("wwww.someURL.com/xml.php"));

    }

请注意,实际的 xml 是包含 xml 结构的 php(来自 sql DB)

 void pestotora_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        XElement doc = XElement.Parse(e.Result);
        listBox1.ItemsSource = from results in doc.Descendants("Data")
                            select new PestotoraPost
                            {
                                ID = results.Element("DataID").Value,
                                Date = results.Element("DataDate").Value,
                                Name=results.Element("DataName").Value,
                                Message=results.Element("DataMessage").Value     
                            };
    }

每次 XML 更改时,我的列表框都不会更新listBox1.UpdateLayout();

关于如何开始实施这个的任何线索/帮助?非常感谢。

更新

namespace pestotora
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            UIload(true);
            WebLoad();

        }

        public ObservableCollection<PestotoraPost> Posts { get; set; }

        public class PestotoraPost
        {
            public string ID { get; set; }
            public string Date { get; set; }
            public string Name { get; set; }
            public string Message { get; set; }
        }


        void WebLoad()
        {

            WebClient pestotora = new WebClient();
            pestotora.DownloadStringCompleted += new DownloadStringCompletedEventHandler(pestotora_DownloadStringCompleted);
            pestotora.DownloadStringAsync(new Uri("www.domain.com/xml.php"));

        }

        void UIload(bool Splash)
        {
            if (Splash == true)
            {

                ApplicationBar.IsVisible = false;
            }
            else
            {

                ApplicationBar.IsVisible = true;
            }
        }

        void pestotora_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            //XDocument doc = XDocument.Parse(e.Result);

            XElement doc = XElement.Parse(e.Result);


            /* listBox1.ItemsSource = from results in doc.Descendants("Data")
                                select new PestotoraPost
                                {
                                    ID = results.Element("DataID").Value,
                                    Date = results.Element("DataDate").Value,
                                    Name=results.Element("DataName").Value,
                                    Message=results.Element("DataMessage").Value

                                }; */
            var list = from results in doc.Descendants("Data")
                       select new PestotoraPost
                       {
                           ID = results.Element("DataID").Value,
                           Date = results.Element("DataDate").Value,
                           Name = results.Element("DataName").Value,
                           Message = results.Element("DataMessage").Value
                       };
            Posts = new ObservableCollection<PestotoraPost>(list);
            foreach (var post in list)
            {
                Posts.Add(post);
            }

            UIload(false);


        }


        private void ApplicationBarMenuItem_Click(object sender, EventArgs e)
        {
            //stckPost.Visibility = System.Windows.Visibility.Visible;

        }



        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Post.xaml", UriKind.Relative));
        }

        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
        {

            listBox1.UpdateLayout();
            //listBox1.ScrollIntoView(listBox1.Items[0]);

            WebLoad();
        }

    }

}
4

1 回答 1

0

1. 如果您使用 MVVM 模式:在 viewModel 中实现 INotifyPropertyChanged 并声明:

private ObservableCollection<PestotoraPost> _posts;
public ObservableCollection<PestotoraPost> Posts 
{
 get{return _posts;} 
 set
  {
     _posts = value;
     RaisePropertyChanged("Posts");
  }
 }

在 xaml 中写:

<ListBox Name="listbox1" ItemsSource="{Binding Posts}"/>

在pestoora_DownloadStringCompleted 方法中:

var list = from results in doc.Descendants("Data")
                            select new PestotoraPost
                            {
                                ID = results.Element("DataID").Value,
                                Date = results.Element("DataDate").Value,
                                Name=results.Element("DataName").Value,
                                Message=results.Element("DataMessage").Value     
                            };
Posts = new ObservableCollection<PestotoraPosts>(list);

2. 如果您在代码隐藏中运行所有内容,只需声明

 public ObservableCollection<PestotoraPosts> Posts {get;set;}

在 Page 构造函数初始化集合中:

 Posts = new ObservableCollection<PestotoraPosts>();

Xaml 将具有相同的声明。在pestotora_DownloadStringCompleted方法中:

var list = from results in doc.Descendants("Data")
                            select new PestotoraPost
                            {
                                ID = results.Element("DataID").Value,
                                Date = results.Element("DataDate").Value,
                                Name=results.Element("DataName").Value,
                                Message=results.Element("DataMessage").Value     
                            };
foreach(var post in list)
{
   Posts.Add(post);
}

我们做了什么?

在 xaml 中,我们设置了一个数据绑定。现在listBox1将从Post收集中获取数据。然后我们创建了新的集合并通知listBox1更新它的视图。(在第一个 vatiant 中)。

在第二个变体中,我们填充了集合,并且由于它是可观察的,它通知了一个列表框。

于 2012-09-11T17:47:11.170 回答