【问题标题】:ADO.NET DataServices with Prism带有 Prism 的 ADO.NET 数据服务
【发布时间】:2009-11-20 01:44:00
【问题描述】:

我已经开始在 silverlight 3 中使用 prism,但是,我们正在尝试实现它以与 ADO.NET DataServices 一起使用。与 Silverlight 一起使用所需的“DataServiceQuery”查询类型需要在查询后触发异步调用。这将打破我们所看到的棱镜图案。 有什么想法可以仅获取要在 Prism Pattern 中使用的查询数据吗?如果我错了,请纠正我!

【问题讨论】:

  • 好吧,问题是我不是直接从视图模型调用数据,而是从服务层调用,但 silverlight 要求我在服务层进行回调......那是我做错了...我想使用 ado.net 数据服务从服务层返回一个可观察的集合...对不起,我对 silverlight 有点不了解。一次消化太多了;)

标签: silverlight prism wcf-data-services


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    这是一个小样本。这有点棘手... complete 事件有时会在 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;
              });
         }
    }
    

    当然这里没有异常处理,但希望你能明白。

    【讨论】:

    • 好吧,beginInvoke 方法丢失了,目前我不知道为什么。但感谢您的回复
    • 在调度程序上?这没有多大意义。
    • 哦,对不起...我忘了包括 CurrentDispatcher。我正在修改代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 2010-09-24
    相关资源
    最近更新 更多