1

我的虚拟机中有 Sessions 属性

private ObservableAsPropertyHelper<IEnumerable<SessionViewModel>> _Sessions;
public IEnumerable<SessionViewModel> Sessions
{
    get { return _Sessions.Value; }
}

我正在尝试像这样在构造函数中设置它

this.WhenAny(x => x.LocationsViewModel.CurrentLocation, x => x.Date, (location, date) => location.Value)
        .Where(x => x != null)
        .Select(x => FilterSessions(x.Id, Date))
        .ToProperty(this, x => x.Sessions);

FilterSessions 看起来像这样

private IEnumerable<SessionViewModel> FilterSessions(Guid locationId, DateTime date)
{
     return _allSessions
        .Where(s => s.SessionLocationId == locationId && s.StartTime.Date == date.Date)
        .Select(s => new SessionViewModel(s));
}

它返回 10 个 SessionViewModel,但 _Sessions 从未设置。

4

2 回答 2

4

这是什么平台?某些平台不允许通过反射设置私有字段。相反,您可以只使用 ToProperty 的返回值:

_Sessions = this.WhenAny(x => x.LocationsViewModel.CurrentLocation, x => x.Date, (location, date) => location.Value)
        .Where(x => x != null)
        .Select(x => FilterSessions(x.Id, Date))
        .ToProperty(this, x => x.Sessions);

或者更好,在 RxUI 5.x 中:

this.WhenAny(x => x.LocationsViewModel.CurrentLocation, x => x.Date, (location, date) => location.Value)
        .Where(x => x != null)
        .Select(x => FilterSessions(x.Id, Date))
        .ToProperty(this, x => x.Sessions, out _Sessions);
于 2013-06-29T21:09:42.860 回答
1

我通过在 subscribe 方法中设置属性来解决这个问题,如下所示:

this.WhenAny(x => x.LocationsViewModel.CurrentLocation, x => x.Date, (location, date) => location.Value)
        .Where(x => x != null)
        .Select(x => FilterSessions(x.Id, Date))
        .Subscribe(x => Sessions = x);
于 2013-06-30T12:12:58.160 回答