使用 WebClient 获取一些数据后,Databind 未更新

发布于 2024-12-27 00:21:00 字数 1212 浏览 0 评论 0原文

我对 Windows Mobile 开发(.net 框架)相当陌生,我一直在尝试找出解决这个问题的解决方案,我已经有一段时间了:

1 - 我有一个 ViewModel,实现 INotifyPropertyChanged 其中包含我的人员列表:

public ObservableCollection<Person> listOfPeople;

2 - 我将 listOfPeople 数据绑定到列表框;

3 - 我在 MainPage.xaml.cs 中创建了一些虚拟数据,它更新了列表框。

4 - 问题是当我尝试从下载的 rss 更新人员信息时:对于每个人员对象,我调用其 Person.updateData() 方法。该方法从 rss feed 中获取一些信息:

internal void updateData()
        {
            WebClient wc = new WebClient();
            wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
            wc.DownloadStringAsync(new Uri(sourceUrl));

        }


        void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            //Parse the data: 
              ...

            //Try to update:

                updateAge(age);
                updateAddress(Address);                
            }

        }

并且这些值在类中更新,但不在 UI 中更新。 在我搜索之后,我发现处理程序在 diff 线程中运行,这可能就是问题所在。 我尝试过

Deployment.Current.Dispatcher.BeginInvoke(delegate { updateSeason(season); });

但没有成功。

谁能帮助我吗?

谢谢

I'm fairly new to Windows Mobile Development (.net framework as well) and I have been trying to figure out a solution for this problem I'm having for quite some time:

1 - I have a ViewModel, implementing INotifyPropertyChanged that contains my list of people:

public ObservableCollection<Person> listOfPeople;

2 - I am databinding listOfPeople to a listbox;

3 - I created some dummy data in my MainPage.xaml.cs and it updates the listbox.

4 - The problem is when I try to update people info from a rss that I download: For every person object I call an its Person.updateData() method. The method fetches some info from an rss feed:

internal void updateData()
        {
            WebClient wc = new WebClient();
            wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
            wc.DownloadStringAsync(new Uri(sourceUrl));

        }


        void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            //Parse the data: 
              ...

            //Try to update:

                updateAge(age);
                updateAddress(Address);                
            }

        }

And the values are updated in the class but not in the UI.
After I searched I found the handler operates in a diff thread, and thats probably the problem.
I tried

Deployment.Current.Dispatcher.BeginInvoke(delegate { updateSeason(season); });

but didnt work.

Can anyone help me?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自此以后,行同陌路 2025-01-03 00:21:00

另一个线程可能不是原因,因为 DownloadStringCompleted 事件处理程序正在创建 WebClient 的线程中运行(在本例中可能是 UI 线程)。

这可能是一个太明显的问题,但是您是否生成了 INotifyPropertyChanged。当您尝试更新 UI 时,ViewModel 中的 PropertyChanged 事件?

Another thread probably is not a reason because DownloadStringCompleted event handler is running in thread in which WebClient was created (in this case probably UI thread).

This may be too obvious question, but have you generated INotifyPropertyChanged.PropertyChanged event in your ViewModel when you are trying to update UI?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文