C# ReactiveCommand:在ThrownExceptions中获取命令参数
在我的场景中,我使用命令从数据库中删除一些内容。我已在WPF中将ReactiveCollection绑定到DataGrid,并使用以下代码处理删除:C# ReactiveCommand:在ThrownExceptions中获取命令参数,c#,wpf,reactiveui,C#,Wpf,Reactiveui,在我的场景中,我使用命令从数据库中删除一些内容。我已在WPF中将ReactiveCollection绑定到DataGrid,并使用以下代码处理删除: RemoveProductCommand = ReactiveCommand.CreateFromTask<Product>(async product => { await _licensingModel.RemoveProduct(product.Id);
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);
});
但是ThrownException呢
我的问题是,在ThrownException中是否有一种内置的方式来获取产品(命令参数)
不,ThrownExceptions是一个IObservable,也就是说,您订阅它只是为了获得一个异常流。它不知道在实际异常发生之前传递给命令的任何命令参数
您需要以某种方式自己存储此参数值,例如通过定义并从Execute方法引发自定义异常。太糟糕了,我希望使用统一的解决方案,无论我如何调用该命令
RemoveProductCommand = ReactiveCommand.CreateFromTask<Product>(async product =>
{
try
{
await _licensingModel.RemoveProduct(product.Id);
}
catch (Exception e)
{
throw new ProductRemoveException(product, e);
}
});
Products.ItemsRemoved.Subscribe(async p =>
{
try
{
await RemoveProductCommand.Execute(p);
}
catch (Exception e)
{
}
});