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> 类型公开以下成员。
构造函数
页首
属性
名称 | 说明 | |
---|---|---|
Continuation | 获取用于返回下一分页结果集的延续对象。 | |
Count | (从 Collection<T> 继承。) | |
Item | (从 Collection<T> 继承。) | |
Items | (从 Collection<T> 继承。) |
页首
方法
页首
事件
名称 | 说明 | |
---|---|---|
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 窗口的根布局控件的 StackPanel 的 DataContext 属性。
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 命名空间