私はやや大規模な WP7 プロジェクトに取り組んでおり、現在のタスクはオフライン機能を実装することです。
デバイスがインターネットに接続されている場合にのみアイテムを表示し、それ以外の場合は空のビューを表示するリストボックスがあります。
接続が変更されたときに起動するイベント ハンドラーを配線しました。接続が確立されている場合は、リスト ボックスに必要なデータを取得します。
問題は、アプリをオフライン モードで実行してから Wi-Fi をオンにすると、リストボックスのデータは更新されますが、UI 自体は更新されないことです。
XAML は次のとおりです。
<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 には、Binding 用の ObservableCollection があります。
public ObservableCollection<ItemCategory> ItemCategories
{
get { return itemCategories; }
set
{
itemCategories = value;
NotifyPropertyChanged("ItemCategories");
}
}
また、デバイスがインターネットに接続されたときに必要なアイテムを取得するバックグラウンド ワーカーと、RunWorkerCompleted メソッドがあります。
void itemCategoryWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
App.ItemCategories = (List<ItemCategory>)e.Result;
ItemCategories = new ObservableCollection<ItemCategory>(App.ItemCategories);
}
そのため、UI に接続されている ItemCategories プロパティは更新されますが、UI 自体は更新されません。