ReactiveUI 中的复合输出属性

Compound Output Properties in ReactiveUI

我有一个关于 reactui 视图模型的问题。

我的视图模型有两个属性,月和年,我希望有一个进一步的输出 属性 DateLabel 可以很好地结合两者并格式化,例如"Jan 2015"。

这第三个 属性 依赖于其他两个,因为任何更改都应该更新,并且任何绑定到此 属性 的内容也应该更新。

阅读 ReactiveUI 文档后,我认为输出 属性 是我所需要的,但我只能在一个或另一个相关 属性 未更改时更新标签。这是通过以下代码实现的。

readonly ObservableAsPropertyHelper<string> dateLabel;

public string DateLabel {
    get { return dateLabel.Value; }
}

this.WhenAnyValue (x => x.Month)
  .Select (x =>  CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName (x) + this.Year)
  .ToProperty (this, x => x.DateLabel, out dateLabel);

this.WhenAnyValue (x => x.Year)
  .Select (x =>  CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName (this.Month) + x)
  .ToProperty (this, x => x.DateLabel, out dateLabel);

在这个例子中,第二个 WhenAnyValue 似乎覆盖了第一个,因此 Year 的任何变化都反映在 DateLabel 的变化中,但对 Month 的任何变化都没有,如果我切换顺序,那么对 Month 的任何变化都会反映出来并且 Year 没有任何变化。

我确定我只是遗漏了一些非常基本的东西,我们将不胜感激任何帮助。

谢谢,埃德

尝试以下操作:

this.WhenAnyValue (x => x.Month, y => y.Year )
.Select (x => String.Format("{0} {1}", CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName (x.Item1), x.Item2))
.ToProperty (this, x => x.DateLabel, out dateLabel);

我想这会如你所愿!