小编Jer*_*ess的帖子

如何使用Silverlight中的Reactive Extensions(Rx)组织这些调用?

我有一些必须按顺序执行的调用.考虑具有Query和Load方法的IService.Query提供了一个小部件列表,并且load提供了一个"默认"小部件.因此,我的服务看起来像这样.

void IService.Query(Action<IEnumerable<Widget>,Exception> callback);
void IService.Load(Action<Widget,Exception> callback); 
Run Code Online (Sandbox Code Playgroud)

考虑到这一点,这里是视图模型的草图:

public class ViewModel : BaseViewModel
{
   public ViewModel()
   {
      Widgets = new ObservableCollection<Widget>();

      WidgetService.Query((widgets,exception) =>
      {
          if (exception != null) 
          {
              throw exception;
          }

          Widgets.Clear();

          foreach(var widget in widgets)
          {
             Widgets.Add(widget);
          }

          WidgetService.Load((defaultWidget,ex) =>
          {
             if (ex != null)
             {
                 throw ex;
             }
             if (defaultWidget != null)
             {
                CurrentWidget = defaultWidget;
             }
          }
      });
   }

   public IService WidgetService { get; set; } // assume this is wired up

   public ObservableCollection<Widget> Widgets { …
Run Code Online (Sandbox Code Playgroud)

silverlight asynchronous system.reactive

7
推荐指数
1
解决办法
1702
查看次数