如何刷新Metro应用程序中的UI? [英] How to refresh the UI in a metro app?
问题描述
我正在XAML页面中使用以下代码:
I'm using this code in a XAML page:
<TextBox ItemsSource="{Binding Posters, Converter={StaticResource collectionToFirstElementConverter}, Mode=TwoWay}" />
Posters是一个ObsevableCollection,我正在使用一个转换器,在其中使用该集合并获取它的第一个元素.
Posters is an ObsevableCollection and I'm using a converter where takes the collection and gets the first element of it.
当我使用异步过程时,文本框在其中接收对象,因此该对象没有任何元素(Count = 0),并调用转换器.
As I'm using async procedures, where the textbox receives the object, this one has no elements (Count=0), and calls the converter.
每次属性添加新元素时,我都尝试更新文本框,但不调用转换器.
I'm trying to update the textbox everytime the property add new elements, but not calls the converter.
我记得在Silverlight或WPF中,存在SourceTrigger或UpdatePropertyChanged,但是在WinRT中,我看不到此模式.
I remember that in Silverlight or WPF, exists SourceTrigger or UpdatePropertyChanged, but in WinRT I can't see this mode.
推荐答案
最简单的方法是修改包含Posters
属性的视图模型.我可以看到两种解决方法(都假设您的视图模型实现了INotifyPropertyChanged
):
The easiest way to achieve that would be to modify your view model containing the Posters
property accordingly. I can see two ways to go about it (both asuming that your view model implements INotifyPropertyChanged
):
- 向
Posters.CollectionChanged
添加事件处理程序,并在其中将INotifyPropertyChanged.PropertyChanged
提升为Posters
. - 添加另一个属性
FirstPoster
,返回Posters
中第一个元素的值.在视图模型中,将事件处理程序添加到Posters.CollectionChanged
,并在其中将INotifyPropertyChanged.PropertyChanged
引发为FirstPoster
.这样,您甚至都不需要转换器.
- Add an event handler to
Posters.CollectionChanged
and inside it raiseINotifyPropertyChanged.PropertyChanged
forPosters
. - Add another property
FirstPoster
returning the value of the first element inPosters
. In the view model add an event handler toPosters.CollectionChanged
and inside it raiseINotifyPropertyChanged.PropertyChanged
forFirstPoster
. This way you don't even need the converter.
我个人更喜欢第二种方法.
I personally like the second approach better.
这篇关于如何刷新Metro应用程序中的UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!