ReactiveUI WhenAnyValue ToPropertyEx 为可为空的小数

Cod*_*dyC 3 c# reactiveui

使用可为空的小数作为变量 ,[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);
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

我需要CustomPrice在输入值时进行更新DiscountPercent和/或DiscountFlat否则需要将null其设置UnitPrice为UnitPriceOriginal. 使用第一块代码时,我收到运行时错误:

System.ArgumentException:“ObservableAsPropertyHelper`1[System.Nullable`1[System.Decimal]]”类型的对象无法转换为“System.Nullable`1[System.Decimal]”类型。'

我尝试了许多不同的方法来解决这个问题,但是在更新可为空变量时我遇到了错误。有没有不同的方法可以在运行时将可为空的小数设置为 Observable?我有什么遗漏的吗?

som*_*dom 5

确保在使用Reactive和ObservableAsProperty属性的项目中,您可以直接引用该ReactiveUI.Fody包;它不能通过另一个项目隐式引用。如果没有这种直接引用,FodyWeavers.xml则不会创建该文件,并且您将从反应属性中得到奇怪的行为。