2016-12-20 59 views
1

在我的场景中,我使用命令从数据库中删除了一些东西。我已经ReactiveCollection势必DataGrid的WPF中,并按照处理删除代码:ReactiveCommand:在ThrownExceptions中获取命令参数

RemoveProductCommand = ReactiveCommand.CreateFromTask<Product>(async product => 
      { 
       await _licensingModel.RemoveProduct(product.Id); 
      }); 


      RemoveProductCommand.ThrownExceptions.Subscribe(async ex => 
      { 
       await ShowToUserInteraction.Handle("Could not remove product "); 
      }); 

      Products.ItemsRemoved.Subscribe(async p => 
      { 
       await RemoveProductCommand.Execute(p); 
      }); 

我的问题是,有没有获得在ThrownExceptions产品(命令参数)内置的方式吗?

我当然可以做这样的事情:

RemoveProductCommand = ReactiveCommand.CreateFromTask<Product>(async product => 
      { 
       try 
       { 
        await _licensingModel.RemoveProduct(product.Id); 
       } 
       catch (Exception e) 
       { 
        throw new ProductRemoveException(product, e); 
       } 

      }); 

我的理由是,我想通知与信息用户“无法删除产品X”。

编辑: 好的,我编辑粘贴在这里的代码,并注意到等待command.Execute()和订阅之间的区别。 是可能的:

Products.ItemsRemoved.Subscribe(async p => 
      { 
       try 
       { 
        await RemoveProductCommand.Execute(p); 
       } 
       catch (Exception e) 
       { 

       } 
      }); 

但什么ThrownExceptions?

回答

1

我的问题是,是否有内置的方法获取产品(命令参数)在ThrownExceptions?

没有,ThrownExceptions是的IObservable <异常>,即你SUBCRIBE它得到的只有例外的流。它不知道在发生实际异常之前传递给命令的任何命令参数。

您将需要以某种方式自己存储此参数值,例如通过从Execute方法中定义并抛出一个自定义Exception。

+0

太糟糕了,我希望统一解决方案,不管我如何调用命令 –