2

这里有一个关于新的 WCF Ria 服务测试版的快速问题:

如果我在代码隐藏中这样做:

EntitySet e = MyContext.Employees

似乎实体集在运行时总是空的?即,如果我想遍历 Employee 实体集。

此外,如果我正在获取实体集的枚举器,我会收到一条错误消息,告诉我枚举器为空或尚未启动。有没有办法从上下文中获取实体集合并遍历它们?

提前致谢!

4

1 回答 1

5

您是否在Completed事件回调中进行了检查?请记住,在 Silverlight 中,所有调用都是异步的。即使您看到在回调之前分配 ItemsSource 的示例代码,它也依赖于这样一个事实:Employees 是数据绑定的 ObservableCollection。

LoadEmployeeCommand()
{
    // The Load method initiates the call to the server
    LoadOperation<Employee> loadOperation = domainContext.Load(domainContext.GetEmployeesQuery());
    // The EntitySet is still empty at this point
    employeeDataGrid.ItemsSource = domainContext.Employees; 
    loadOperation.Completed += EmployeeLoadOperationCompleted;
}

private void EmployeeLoadOperationCompleted(object sender, EventArgs e)
{
    // Don't need to reassign now but at this point the collection should be populated
    employeeDataGrid.ItemsSource = domainContext.Employees;
}
于 2009-12-10T21:40:32.237 回答