Windows Phone 7.1 アプリケーションで。ピボット コントロール内にリスト ボックスがあります。私のリストボックスには、Web サービスからの OData を使用してデータが取り込まれます。http://services.odata.org/Northwind/Northwind.svc/にあるサービスを使用しています私のテストのために。リストボックスのデータを更新できません。たとえば、アプリケーションが読み込まれると、アプリは OrderID 10248 と 10249 のデータを取得します。ユーザーがアプリケーション バーのボタンを押すと、OrderID 10250 と 10251 のレコードを取得したいと思います。データを取得するための呼び出しを行うと、 、アプリケーションからエラーが発生せず、UI のデータが更新されません。DataServiceCollection は、それ自体が INotifyPropertChanged を実装する ObservableCollection を実装していることを読んだことから、コレクションが変更されたときに UI を更新する必要があることを理解しています。しかし、そうではありません。
GridView を使用して WPF アプリケーションでこれをテストしたところ、UI は新しいデータで問題なく更新されます。ただし、WPF の呼び出しは非同期ではないことは理解しています。どんな助けでも大歓迎です。
以下は、データを取得するために ViewModel で使用しているコードです。
private NorthwindEntities context;
private const string svcUri = "http://services.odata.org/Northwind/Northwind.svc/";
public MainViewModel()
{
List<string> nums = new List<string>() { "10248", "10249" };
GetDataFromService(nums);
}
public void GetDataFromService(List<string> zNumbers)
{
try
{
string partQuery = "Orders()?$filter =";
if (zNumbers.Count > 0)
{
foreach (var item in zNumbers)
{
partQuery += "(OrderID eq " + item + ") or ";
}
partQuery = partQuery.Substring(0, partQuery.Length - 3).Trim();
}
// Initialize the context for the data service.
context = new NorthwindEntities(new Uri(svcUri));
Uri queryUri = new Uri(partQuery, UriKind.Relative);
trackedCustomers = new DataServiceCollection<Order>(context);
trackedCustomers.LoadAsync(queryUri);
}
catch (DataServiceQueryException ex)
{
MessageBox.Show("The query could not be completed:\n" + ex.ToString());
}
catch (InvalidOperationException ex)
{
MessageBox.Show("The following error occurred:\n" + ex.ToString());
}
}
private DataServiceCollection<Order> trackedCustomers;
public DataServiceCollection<Order> TrackedCustomers
{
get { return trackedCustomers; }
set
{
if (value != trackedCustomers)
{
trackedCustomers = value;
NotifyPropertyChanged("TrackedCustomers");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
そして、ここに私の MainPage.xaml の XAML があります
<Grid x:Name="LayoutRoot" Background="Transparent">
<!--Pivot Control-->
<controls:Pivot Title="MY APPLICATION">
<!--Pivot item one-->
<controls:PivotItem Header="first">
<!--Double line list with text wrapping-->
<ListBox x:Name="FirstListBox" Margin="0,0,-12,0" ItemsSource="{Binding TrackedCustomers, Mode=OneWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="78">
<TextBlock Text="{Binding OrderID}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding Freight}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</controls:PivotItem>
</controls:Pivot>
</Grid>