ReactiveUI - 多个 InvokeCommand 不适用于单个可观察对象

ReactiveUI - Multiple InvokeCommand is not working for a single observable

我的 DetailsViewModel 监视所选 EnvelopeViewModel 的变化并更新 DetailsViewModel 的 AgendaItemId。

MessageBus.Current.Listen(Of EnvelopeViewModel) _
        .Where(Function(x) x IsNot Nothing) _
        .Select(Function(x) x.AgendaItemID) _
        .ToPropertyEx(Me, Function(vm) vm.AgendaItemId)

我使用从 _agendaItemService 返回的 AgendaItem 更新 DetailsViewModel。我相信这会引起问题。

UpdateViewModel = ReactiveCommand.CreateAsyncTask(Function(x) _agendaItemService.FindAsync(AgendaItemId))
UpdateViewModel.SubscribeOn(RxApp.MainThreadScheduler).Subscribe(Sub(agendaItem) UpdateDetails(agendaItem))
UpdateViewModel.ThrownExceptions.Subscribe(Sub(ex) Log.ErrorException("LoadViewModel", ex))

WhenAnyValue(Function(vm) vm.AgendaItemId) _
  .Where(Function(agendaItemId) agendaItemId > 0) _
  .InvokeCommand(UpdateViewModel)

Private Sub UpdateDetails(agendaItem As AgendaItem)

    _agendaItem = agendaItem

    PrimaryAgendaCategoryId = _agendaItem.AgendaCategory.ParentId

    PrimaryAmount = agendaItem.Amounts.Where(Function(x) x.AmountType = AmountType.Primary).Sum(Function(x) x.Value)
    SecondaryAmount = agendaItem.Amounts.Where(Function(x) x.AmountType = AmountType.Secondary).Sum(Function(x) x.Value)
    LocalMatchAmount = agendaItem.Amounts.Where(Function(x) x.AmountType = AmountType.LocalMatch).Sum(Function(x) x.Value)
    ChangeOrderTotalAmount = agendaItem.Amounts.Where(Function(x) x.AmountType = AmountType.ChangeOrder).Sum(Function(x) x.Value)
    ProjectTotalAmount = agendaItem.Amounts.Sum(Function(x) x.Value)

    Mapper.Map(_agendaItem, Me)

    'TODO fix this attempt at getting save button to refresh when amounts are changed and saved.
    Name = ""
    Name = _agendaItem.Name

End Sub

这似乎确实有效。 DetailsView 使用新值更新。失败的地方是当我尝试更新 DepartmentContacts 时。

GetDepartmentContacts = ReactiveCommand.CreateAsyncTask(Function(x) _departmentService.GetDepartmentContactsAsync(AgendaItemId))
GetDepartmentContacts.ToPropertyEx(Me, Function(x) x.DepartmentContacts, New BindingList(Of PersonContactViewModel))
GetDepartmentContacts.ThrownExceptions.Subscribe(Sub(ex) Log.ErrorException("GetDepartmentContacts", ex))

WhenAnyValue(Function(vm) vm.AgendaItemId) _
        .Where(Function(id) id > 0) _
        .InvokeCommand(GetDepartmentContacts)

ObservableAsPropertyHelper DepartmentContacts 无法更新视图。

但是,如果我注释掉 UpdateViewModel 命令的调用,则 DepartmentContacts 会毫无问题地更新。

ReactiveUI 中一定有更好的方法来更新我的 ViewModel。

我相信我在 Combining Commands 中找到了答案。

我认为问题是我不能在同一个可观察对象上使用 InvokeCommand 两次。 ReactiveCommand.CreateCombined 让我将这两个命令合并为一个可以调用的命令,它将处理调用分配给它的两个命令。

UpdateViewModel = ReactiveCommand.CreateAsyncTask(Function(x) _agendaItemService.FindAsync(AgendaItemId))
UpdateViewModel.SubscribeOn(RxApp.MainThreadScheduler).Subscribe(Sub(agendaItem) UpdateDetails(agendaItem))
UpdateViewModel.ThrownExceptions.Subscribe(Sub(ex) Log.ErrorException("LoadViewModel", ex))

GetDepartmentContacts = ReactiveCommand.CreateAsyncTask(Function(x) _departmentService.GetDepartmentContactsAsync(AgendaItemId))
GetDepartmentContacts.ToPropertyEx(Me, Function(x) x.DepartmentContacts, New BindingList(Of PersonContactViewModel))
GetDepartmentContacts.ThrownExceptions.Subscribe(Sub(ex) Log.ErrorException("GetDepartmentContacts", ex))

RefreshAgendaItemValues = ReactiveCommand.CreateCombined(RefreshViewModel, RefreshDepartmentContacts)

WhenAnyValue(Function(vm) vm.AgendaItemId) _
    .Where(Function(id) id > 0) _
    .InvokeCommand(RefreshAgendaItemValues)