Silverlight 数据源更改时列表框不更新内容
我正在处理一个有点大的WP7项目,我当前的任务是实现离线功能 我有一个列表框,它应该只在设备连接到internet时显示项目,否则显示空视图 我有一个连接好的事件处理程序,当连接发生变化时触发,如果我有连接,我将检索列表框所需的数据 问题是,当我在脱机模式下运行应用程序,然后打开Wi-fi时,列表框的数据会更新,但UI本身不会更新 以下是XAML:Silverlight 数据源更改时列表框不更新内容,silverlight,windows-phone-7,Silverlight,Windows Phone 7,我正在处理一个有点大的WP7项目,我当前的任务是实现离线功能 我有一个列表框,它应该只在设备连接到internet时显示项目,否则显示空视图 我有一个连接好的事件处理程序,当连接发生变化时触发,如果我有连接,我将检索列表框所需的数据 问题是,当我在脱机模式下运行应用程序,然后打开Wi-fi时,列表框的数据会更新,但UI本身不会更新 以下是XAML: <ListBox Name="lstItemCategories" ItemsSource="{Binding ItemCategories,
<ListBox Name="lstItemCategories" ItemsSource="{Binding ItemCategories, Mode=TwoWay}" SelectedItem="{Binding SelectedItemCategory, Mode=TwoWay}" Margin="0,-15,0,60" Tap="lstItemCategories_Tap">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,0" Height="78" Width="432">
<Image Height="70" Width="70" HorizontalAlignment="Right" Name="image1" Stretch="Uniform" Source="{Binding ImagePath}" />
<ListBoxItem toolkit:TiltEffect.IsTiltEnabled="True">
<TextBlock Text="{Binding Description}" VerticalAlignment="Center" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" Foreground="{StaticResource darkGrey}"/>
</ListBoxItem>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
在ViewModel中,我有一个用于绑定的ObservableCollection:
public ObservableCollection<ItemCategory> ItemCategories
{
get { return itemCategories; }
set
{
itemCategories = value;
NotifyPropertyChanged("ItemCategories");
}
}
公共可见收集项目类别
{
获取{return itemcegories;}
设置
{
项目类别=价值;
NotifyPropertyChanged(“项目类别”);
}
}
我有一个backgroundworker,它可以在设备连接到internet后检索我需要的项目,还有RunWorkerCompleted方法:
void itemCategoryWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
App.ItemCategories = (List<ItemCategory>)e.Result;
ItemCategories = new ObservableCollection<ItemCategory>(App.ItemCategories);
}
void itemCategoryWorker\u RunWorkerCompleted(对象发送方,RunWorkerCompletedEventArgs e)
{
App.itemcegories=(列表)e.Result;
ItemCategories=新的ObservableCollection(App.ItemCategories);
}
因此,连接到UI的ItemCategories属性会被更新,但UI本身并没有得到更新。必须在UI线程上调用对ItemCategories的更改:
((MainView)view).Dispatcher.BeginInvoke(() =>
{
ItemCategories = new ObservableCollection<ItemCategory>(App.ItemCategories);
});
((主视图)视图).Dispatcher.BeginInvoke(()=>
{
ItemCategories=新的ObservableCollection(App.ItemCategories);
});