c# - ReactiveUI WhenAnyValue ToPropertyEx 到一个可为空的小数
问题描述
使用可以为空的小数作为变量,[Reactive] public decimal? CustomPrice { get; set; }
我正在尝试使用以下内容对其进行更新:
this.WhenAnyValue(x => x.DiscountPercent, x => x.DiscountFlat, x=>x.UnitPriceOriginal,
(percent, flat, orig) => ((percent > 0) || (flat > 0)) ? orig * percent - flat : (decimal?)null)
.ToPropertyEx(this, x => x.CustomPrice);
DiscountPercent DiscountFlat UnitPriceOriginal
都是十进制变量。DiscountPercent DiscountFlat
从数据库或用户输入字段中设置。CustomPrice
也可以从用户输入字段进行编辑,该字段一直在努力更新CustomPrice
. CustomPrice
然后使用以下方法设置十进制值UnitPrice
:
this.WhenAnyValue(x => x.CustomPrice, x => x.UnitPriceOriginal,
(custom, orig) => custom ?? orig)
.ToPropertyEx(this, x => x.UnitPrice);
我需要在CustomPrice
输入值时更新DiscountPercent
和/或DiscountFlat
需要将null
设置UnitPrice
为UnitPriceOriginal
. 使用第一块代码,我得到一个运行时错误:
System.ArgumentException:“ObservableAsPropertyHelper`1[System.Nullable`1[System.Decimal]] 类型的对象”无法转换为“System.Nullable`1[System.Decimal]”类型。
我尝试了许多不同的方法来解决这个问题,但是在更新一个可为空的变量时我遇到了错误。有没有不同的方法可以在运行时将可为空的小数设置为 Observable?有什么我想念的吗?
解决方案
确保在您使用Reactive
和ObservableAsProperty
属性的项目中,您可以直接引用ReactiveUI.Fody
包;它不能被另一个项目隐式引用。如果没有这种直接引用,FodyWeavers.xml
则不会创建文件,并且您将从反应属性中获得奇怪的行为。
推荐阅读
- php - 在 DB:raw 查询中使用时区
- python - 无法将“位”安装到 conda 环境中。错误:无法为使用 PEP 517 且无法直接安装的 coincurve 构建轮子
- reactjs - 如何使用 Axios 获取数据并将其存储在 Redux 存储中?
- amazon-web-services - 如何将日志从 cloudwatch 发送到 ES
- .net - 无法在 .NET Framework 4.7.2 中配置 System.Net
- angular - Angular订阅循环孩子多次调用
- android - try catch 在firebase Auth颤振中不起作用
- apache-superset - 如何在没有 iframe 的 Web 应用程序中显示超集仪表板?
- elasticsearch - 嵌套对象的 Elasticsearch 映射
- php - 我想在 count = 1 时禁用删除按钮,我想在 count 等于 0 时启用删除按钮