ReactiveCommand:在 ThrownExceptions 中获取命令参数
ReactiveCommand: get command parameter in ThrownExceptions
在我的场景中,我使用命令从数据库中删除一些东西。我将 ReactiveCollection 绑定到 WPF 中的 DataGrid 并使用以下代码处理删除:
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);
}
});
我的理由是我想通知用户信息"Could not remove product X"。
编辑:
好吧,我编辑了用于粘贴的代码,并注意到等待 command.Execute() 和订阅之间的区别。
可以:
Products.ItemsRemoved.Subscribe(async p =>
{
try
{
await RemoveProductCommand.Execute(p);
}
catch (Exception e)
{
}
});
但是抛出异常呢?
My question is, is there a built-in way for getting product (command argument) in ThrownExceptions?
不,ThrownExceptions 是一个 IObservable,即您订阅它以仅获取异常流。它不知道在实际异常发生之前传递给命令的任何命令参数。
您需要自己以某种方式存储此参数值,例如通过从您的 Execute 方法定义和抛出自定义异常。
在我的场景中,我使用命令从数据库中删除一些东西。我将 ReactiveCollection 绑定到 WPF 中的 DataGrid 并使用以下代码处理删除:
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);
}
});
我的理由是我想通知用户信息"Could not remove product X"。
编辑: 好吧,我编辑了用于粘贴的代码,并注意到等待 command.Execute() 和订阅之间的区别。 可以:
Products.ItemsRemoved.Subscribe(async p =>
{
try
{
await RemoveProductCommand.Execute(p);
}
catch (Exception e)
{
}
});
但是抛出异常呢?
My question is, is there a built-in way for getting product (command argument) in ThrownExceptions?
不,ThrownExceptions 是一个 IObservable
您需要自己以某种方式存储此参数值,例如通过从您的 Execute 方法定义和抛出自定义异常。