2009-11-18 103 views
0

我已经开始使用silverlight 3的棱镜,但是,我们正试图实现它与ADO.NET DataServices一起工作。需要与Silverlight一起使用的“DataServiceQuery”查询类型需要在查询后触发Asyncronous调用。通过我所能看到的,这将打破棱镜模式。 任何想法只获取在棱镜模式中使用的查询数据?如果我错了,请纠正我的错误!ADO.NET DataServices与棱镜

+0

嗯,问题是我没有直接从viewmodel调用数据,而是从服务层调用,但silverlight要求我在服务层中进行回调......这就是我没有得到正确的... 我想从服务层返回一个可观察的集合与ado.net数据服务...对不起,我有点在silverlight nOOb。太消化一次;) – Diego 2009-11-18 23:35:22

+0

这是怎么回事? – 2009-12-03 07:19:39

回答

1

对服务器进行异步调用不会中断“棱镜模式”。当你的视图需要查询服务器时,它的viewmodel触发一个异步请求并提供回调。一旦调用回调,它将处理结果并更新它公开给视图的任何属性。这将导致视图更新根据您在xaml中设置的绑定。

0

PL是完全正确的。 Prism鼓励与ADO.NET数据服务不兼容的模式。只有几件事你应该知道。

这是一个小样本。这是一个有点棘手......整个事件有时会触发UI线程之外,所以你必须与调度程序来处理它(至少在SL2你所做的那样):

public class MyViewModel : BaseViewModel 
{ 

    public Customer CustomerResult 
    { 
     ... 
    } 

    NorthwindEntities svcContext = null; 
    public MyViewModel() 
    { 
      svcContext = 
       new NorthwindEntities(new Uri("Northwind.svc", UriKind.Relative)); 

      DataServiceQuery<Customers> query = 
       svcContext.Customers.Expand("Orders"); 

      // Begin the query execution. 
       query.BeginExecute(WorkComplete, query); 

    } 



    private void WorkComplete(IAsyncResult result) 
    { 
      DataServiceQuery<Customers> query = 
        result.AsyncState as DataServiceQuery<Customers>; 

      Customers returnedCustomer = 
        query.EndExecute(result).FirstOrDefault(); 

      //Execute with the dispatcher 
      Dispatcher.CurrentDispatcher.BeginInvoke(() => 
      { 
       CustomerResult = returnedCustomer; 
      }); 
    } 
} 

当然并无异常在这里处理,但你希望得到的图片。

+0

那么,beginInvoke方法丢失,并在我没有搞清楚为什么。但感谢回复 – Diego 2009-11-19 17:03:23

+0

关于调度程序?这没有什么意义。 – 2009-11-19 17:42:08

+0

对不起...我忘了包含CurrentDispatcher。我在修改代码。 – 2009-11-19 17:43:48