DataServiceCollection<T> 类

表示一个动态实体集合,可在添加、移除项目或刷新列表时提供通知。

继承层次结构

System.Object
  System.Collections.ObjectModel.Collection<T>
    System.Collections.ObjectModel.ObservableCollection<T>
      System.Data.Services.Client.DataServiceCollection<T>

命名空间:  System.Data.Services.Client
程序集:  Microsoft.Data.Services.Client(在 Microsoft.Data.Services.Client.dll 中)

语法

声明
Public Class DataServiceCollection(Of T) _
    Inherits ObservableCollection(Of T)
用法
Dim instance As DataServiceCollection(Of T)
public class DataServiceCollection<T> : ObservableCollection<T>
generic<typename T>
public ref class DataServiceCollection : public ObservableCollection<T>
type DataServiceCollection<'T> =  
    class
        inherit ObservableCollection<'T>
    end
JScript 不支持一般类型和方法。

类型参数

  • T
    一个实体类型。

DataServiceCollection<T> 类型公开以下成员。

构造函数

  名称 说明
公共方法 DataServiceCollection<T>() 创建 DataServiceCollection<T> 类的新实例。
公共方法 DataServiceCollection<T>(IEnumerable<T>) 创建基于查询执行的 DataServiceCollection<T> 类的新实例。
公共方法 DataServiceCollection<T>(DataServiceContext) 创建 DataServiceCollection<T> 类的新实例,该实例使用指定的 DataServiceContext
公共方法 DataServiceCollection<T>(IEnumerable<T>, TrackingMode) 创建基于查询执行并使用指定跟踪模式的 DataServiceCollection<T> 类的新实例。
公共方法 DataServiceCollection<T>(DataServiceContext, String, Func<EntityChangedParams, Boolean>, Func<EntityCollectionChangedParams, Boolean>) 创建使用所提供更改方法委托并使用指定的 DataServiceContext 的 DataServiceCollection<T> 类的新实例。
公共方法 DataServiceCollection<T>(IEnumerable<T>, TrackingMode, String, Func<EntityChangedParams, Boolean>, Func<EntityCollectionChangedParams, Boolean>) 创建基于查询执行并使用所提供更改方法委托的 DataServiceCollection<T> 类的新实例。
公共方法 DataServiceCollection<T>(DataServiceContext, IEnumerable<T>, TrackingMode, String, Func<EntityChangedParams, Boolean>, Func<EntityCollectionChangedParams, Boolean>) 创建基于查询执行、使用所提供的更改方法委托并使用所提供的 DataServiceContext 的 DataServiceCollection<T> 类的新实例。

页首

属性

  名称 说明
公共属性 Continuation 获取用于返回下一分页结果集的延续对象。
公共属性 Count (从 Collection<T> 继承。)
公共属性 Item (从 Collection<T> 继承。)
受保护属性 Items (从 Collection<T> 继承。)

页首

方法

  名称 说明
公共方法 Add (从 Collection<T> 继承。)
受保护方法 BlockReentrancy (从 ObservableCollection<T> 继承。)
受保护方法 CheckReentrancy (从 ObservableCollection<T> 继承。)
公共方法 Clear() (从 Collection<T> 继承。)
公共方法 Clear(Boolean) 移除集合中的所有项,并选择性地分离 DataServiceContext 中的所有项。
受保护方法 ClearItems (从 ObservableCollection<T> 继承。)
公共方法 Contains (从 Collection<T> 继承。)
公共方法 CopyTo (从 Collection<T> 继承。)
公共方法 Detach 禁止 DataServiceContext 跟踪集合中的所有项。
公共方法 Equals (从 Object 继承。)
受保护方法 Finalize (从 Object 继承。)
公共方法 GetEnumerator (从 Collection<T> 继承。)
公共方法 GetHashCode (从 Object 继承。)
公共方法 GetType (从 Object 继承。)
公共方法 IndexOf (从 Collection<T> 继承。)
公共方法 Insert (从 Collection<T> 继承。)
受保护方法 InsertItem 将指定项添加到集合的指定索引位置。 (覆盖 ObservableCollection<T>.InsertItem(Int32, T)。)
公共方法 Load(IEnumerable<T>) 将实体对象的集合加载到集合中。 用于 Silverlight 的 WCF Data Services 5.0 客户端不支持。
公共方法 Load(T) 将单个实体对象加载到集合中。 用于 Silverlight 的 WCF Data Services 5.0 客户端不支持。
公共方法 LoadAsync() 当集合表示实体的导航属性时,以异步方式将项加载到该集合中。 只有 Silverlight 的 WCF Data Services 5.0 客户端支持。
公共方法 LoadAsync(IQueryable<T>) 执行 DataServiceQuery<TElement>,以异步方式加载集合。 只有 Silverlight 的 WCF Data Services 5.0 客户端支持。
公共方法 LoadNextPartialSetAsync 将下一页数据加载到集合中。 只有 Silverlight 的 WCF Data Services 5.0 客户端支持。
受保护方法 MemberwiseClone (从 Object 继承。)
公共方法 Move (从 ObservableCollection<T> 继承。)
受保护方法 MoveItem (从 ObservableCollection<T> 继承。)
受保护方法 OnCollectionChanged (从 ObservableCollection<T> 继承。)
受保护方法 OnPropertyChanged (从 ObservableCollection<T> 继承。)
公共方法 Remove (从 Collection<T> 继承。)
公共方法 RemoveAt (从 Collection<T> 继承。)
受保护方法 RemoveItem (从 ObservableCollection<T> 继承。)
受保护方法 SetItem (从 ObservableCollection<T> 继承。)
公共方法 ToString (从 Object 继承。)

页首

事件

  名称 说明
公共事件 CollectionChanged (从 ObservableCollection<T> 继承。)
公共事件 LoadCompleted 在异步加载操作完成时发生。 只有 Silverlight 的 WCF Data Services 5.0 客户端支持。
受保护事件 PropertyChanged (从 ObservableCollection<T> 继承。)

页首

显式接口实现

  名称 说明
显式接口实现私有方法 IList.Add (从 Collection<T> 继承。)
显式接口实现私有方法 IList.Contains (从 Collection<T> 继承。)
显式接口实现私有方法 ICollection.CopyTo (从 Collection<T> 继承。)
显式接口实现私有方法 IEnumerable.GetEnumerator (从 Collection<T> 继承。)
显式接口实现私有方法 IList.IndexOf (从 Collection<T> 继承。)
显式接口实现私有方法 IList.Insert (从 Collection<T> 继承。)
显式接口实现私有属性 IList.IsFixedSize (从 Collection<T> 继承。)
显式接口实现私有属性 ICollection<T>.IsReadOnly (从 Collection<T> 继承。)
显式接口实现私有属性 IList.IsReadOnly (从 Collection<T> 继承。)
显式接口实现私有属性 ICollection.IsSynchronized (从 Collection<T> 继承。)
显式接口实现私有属性 IList.Item (从 Collection<T> 继承。)
显式接口实现私有事件 INotifyPropertyChanged.PropertyChanged (从 ObservableCollection<T> 继承。)
显式接口实现私有方法 IList.Remove (从 Collection<T> 继承。)
显式接口实现私有属性 ICollection.SyncRoot (从 Collection<T> 继承。)

页首

注释

WCF 数据服务 提供了 DataServiceCollection<T> 类以支持在客户端应用程序中将数据绑定到控件。 此类继承自 ObservableCollection<T> 类,该类实现了 INotifyCollectionChanged 接口,并且是基于 Windows Presentation Foundation (WPF) 和 Silverlight 的应用程序的主数据绑定机制。

可以通过使用实现了 IEnumerable<T> 接口的任何集合来加载 ObservableCollection<T> 绑定集合。 加载到绑定集合中的项必须实现 INotifyPropertyChanged 接口。 有关详细信息,请参阅将数据绑定到控件(WCF 数据服务)

示例

下面的示例摘自在 WPF 中定义 SalesOrders 窗口的可扩展应用程序标记语言 (XAML) 页的代码隐藏页。 加载此窗口时,会根据查询结果创建 DataServiceCollection<T>,该查询将返回按国家/地区进行筛选的具有相关对象的客户。 此结果绑定到作为 WPF 窗口的根布局控件的 StackPanelDataContext 属性。

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports System.Data.Services.Client
Imports NorthwindClient.Northwind

Partial Public Class CustomerOrdersWpf
    Inherits Window

    Private context As NorthwindEntities
    Private trackedCustomers As DataServiceCollection(Of Customer)
    Private Const customerCountry As String = "Germany"
    Private Const svcUri As String = "https://localhost:12345/Northwind.svc/"
    Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Try
            ' Initialize the context for the data service.
            context = New NorthwindEntities(New Uri(svcUri))

            ' Create a LINQ query that returns customers with related orders.
            Dim customerQuery = From cust In context.Customers.Expand("Orders") _
                                    Where cust.Country = customerCountry _
                                    Select cust

                ' Create a new collection for binding based on the LINQ query.
            trackedCustomers = New DataServiceCollection(Of Customer)(customerQuery)

                ' Bind the root StackPanel element to the collection
                ' related object binding paths are defined in the XAML.
            Me.LayoutRoot.DataContext = trackedCustomers
        Catch ex As DataServiceQueryException
            MessageBox.Show("The query could not be completed:\n" + ex.ToString())
        Catch ex As InvalidOperationException
            MessageBox.Show("The following error occurred:\n" + ex.ToString())
        End Try
    End Sub
    Private Sub saveChangesButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        ' Save changes to the data service.
        context.SaveChanges()
    End Sub
End Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.Services.Client;
using NorthwindClient.Northwind;

namespace NorthwindClient
{
    public partial class CustomerOrdersWpf : Window
    {
        private NorthwindEntities context;
        private DataServiceCollection<Customer> trackedCustomers;
        private const string customerCountry = "Germany";
        private const string svcUri = "https://localhost:12345/Northwind.svc/";

        public CustomerOrdersWpf()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                // Initialize the context for the data service.
                context = new NorthwindEntities(new Uri(svcUri));

                // Create a LINQ query that returns customers with related orders.
                var customerQuery = from cust in context.Customers.Expand("Orders")
                                    where cust.Country == customerCountry
                                    select cust;

                // Create a new collection for binding based on the LINQ query.
                trackedCustomers = new DataServiceCollection<Customer>(customerQuery);

                // Bind the root StackPanel element to the collection;
                // related object binding paths are defined in the XAML.
                LayoutRoot.DataContext = trackedCustomers;
            }
            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 void saveChangesButton_Click(object sender, RoutedEventArgs e)
        {
            // Save changes to the data service.
            context.SaveChanges();
        }
    }
}

以下 XAML 在 WPF 中为上一个示例定义 SalesOrders 窗口。

    <Window x:Class="CustomerOrdersWpf"
             xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="https://schemas.openxmlformats.org/markup-compatibility/2006" 
             Height="423" Width="679" Loaded="Window_Loaded">
    <StackPanel Orientation="Vertical" Height="Auto" Name="LayoutRoot" Width="Auto">
        <Label Content="Customer ID" Margin="20,0,0,0" />
        <ComboBox Name="customerIDComboBox" DisplayMemberPath="CustomerID" ItemsSource="{Binding}" 
                  IsSynchronizedWithCurrentItem="True" SelectedIndex="0" Height="23" Width="120" 
                  HorizontalAlignment="Left" Margin="20,0,0,0" VerticalAlignment="Center" />
        <ListView ItemsSource="{Binding Path=Orders}" Name="ordersDataGrid" Margin="34,46,34,50">
            <ListView.View>
                <GridView AllowsColumnReorder="False" ColumnHeaderToolTip="Line Items">
                    <GridViewColumn DisplayMemberBinding="{Binding Path=OrderID, Mode=OneWay}" 
                        Header="Order ID" Width="50"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=OrderDate, Mode=TwoWay}" 
                        Header="Order Date" Width="50"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Path=Freight, Mode=TwoWay}" 
                        Header="Freight Cost" Width="50"/>
                </GridView>
            </ListView.View>
        </ListView>
        <Button Name="saveChangesButton" Content="Save Changes" Click="saveChangesButton_Click" 
                Width="80" Height="30" Margin="450,0,0,0"/>
    </StackPanel>
</Window>

线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。不保证所有实例成员都是线程安全的。

请参阅

参考

System.Data.Services.Client 命名空间

其他资源

将数据绑定到控件(WCF 数据服务)