WP7.1 - 如何使用 oData 刷新列表框



在我的Windows Phone 7.1应用程序中。我在透视控件内有一个列表框。我的列表框使用来自 Web 服务的 OData 填充数据。我正在使用在 http://services.odata.org/Northwind/Northwind.svc/找到的服务进行测试。我在刷新列表框中的数据时遇到问题。例如,当应用程序加载时,应用程序会提取 OrderID 10248 和 10249 的数据。现在,如果用户按下应用程序栏中的按钮,我想获取 OrderID 10250 和 10251 的记录。当我调用以获取数据时,我不会从应用程序收到任何错误,并且 UI 中的数据不会刷新。我从阅读中了解到,DataServiceCollection实现了ObservableCollection,它本身实现了INotifyPropertChanged,因此我的UI应该在集合更改时刷新。但事实并非如此。

我已经使用 GridView 在 WPF 应用程序中对此进行了测试,并且 UI 刷新与新数据很好。我知道WPF中的调用不是异步的。任何帮助,不胜感激。

下面是我在视图模型中用于获取数据的代码。

    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>

是否有在 LoadAsync 完成后调用的处理程序? 如果是这样,那就是您需要对公共属性 TrackedCustomers 进行分配的时候。

或者,可能更改这些行以使用公共属性:

        trackedCustomers = new DataServiceCollection<Order>(context);
        trackedCustomers.LoadAsync(queryUri);

问题是您正在将后端集合trackedCustomers更改为一个新对象,而不会告诉 UI 它正在更改。UI 绑定到对象的第一个实例,而你要丢弃该对象。您需要执行以下两项操作之一

清除后备集合:

trackedCustomers.Clear();
trackedCustomers.LoadAsync(queryUri);

或使用公开的属性

TrackedCustomers= new DataServiceCollection<Order>(context);
TrackedCustomers.LoadAsync(queryUri);

最新更新