我有一个ObservableCollection
,它是一个实现INotifyPropertyChanged
界面的View Models的集合......
public class FeedsViewModel : ObservableCollection<FeedViewModel>
{
}
(我没有包含FeedViewModel
的代码,但它非常标准INotifyPropertyChanged
并且在独立测试时绑定。
我在FeedsViewModel
控件中使用ItemsSource
的{{1}}个实例...
LongListSelector
但是,当我在运行时向ObservableCollection添加内容时,UI不会随更改而更新......
<UserControl.Resources>
<viewModel:FeedStatusFeedbackConverter x:Key="ProgressStatusColorConverter"/>
<viewModel:FeedStatusFeedbackConverter x:Key="TitleStatusColorConverter"/>
<viewModel:FeedsViewModel x:Key="FeedsViewModel"/>
</UserControl.Resources>
<phone:LongListSelector ItemsSource="{Binding Source={StaticResource FeedsViewModel}}">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<Grid x:Name="LayoutRoot"
Background="{StaticResource PhoneChromeBrush}"
DataContext="{TemplateBinding}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Path=Name}"
Grid.Row="0"
Style="{StaticResource PhoneTextLargeStyle}"
Foreground="{Binding Path=Status, Converter={StaticResource TitleStatusColorConverter}}"/>
<TextBlock Text="{Binding Path=Uri}"
Grid.Row="1"
Style="{StaticResource PhoneTextNormalStyle}"
TextTrimming="WordEllipsis" />
<ProgressBar Grid.Row="2"
Maximum="{Binding Path=ItemCount}"
Value="{Binding Path=ItemProgress}"
Foreground="{Binding Path=Status, Converter={StaticResource ProgressStatusColorConverter}}">
</ProgressBar>
</Grid>
<Grid Grid.Column="1">
<Image Name="Delete"
Source="/Assets/Buttons/delete.png"
Width="48"
Height="48"
Margin="5"
MouseLeftButtonDown="Press"
MouseLeftButtonUp="Release"/>
</Grid>
</Grid>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
在将新视图模型添加到private void AddFeedClick(object sender, EventArgs e)
{
var task = FeedModel.Load("Reddit");
var awaiter = task.GetAwaiter();
awaiter.OnCompleted(() =>
{
var model = awaiter.GetResult();
FeedModel.Save(model);
((ObservableCollection<FeedViewModel>)Resources["FeedsViewModel"]).Add(new FeedViewModel(model));
});
}
并检查集合时命中断点,表明集合事件的ObservableCollection
没有处理程序(为空)。
如何让LongListSelector从ObservableCollection更新自己?
答案 0 :(得分:1)
答案 1 :(得分:1)
我通过将AddFeedClick
中的广告素材从(ObservableCollection<FeedViewModel>
更改为FeedsViewModel
来解决此问题,这是该项目的实际类型。虽然没有发生无效的演员,但在这里肯定有一些模糊的OO功能。我怀疑该集合实际上有两个阴影事件,一个用于ObservableCollection
类型,另一个用于扩展类型,但我不确定。