首页 > 解决方案 > ReactiveUI - 当 ToProperty 在同一个 Observable 上正常时 ReactiveCommand 失败

问题描述

我有一个 ReactiveCommand 引发以下错误。

实现 IHandleObservableErrors 的对象(通常是 ReactiveCommand 或 ObservableAsPropertyHelper)发生错误,从而破坏了其可观察管道。为防止这种情况,请确保管道不会出错,或订阅相关对象的 ThrownExceptions 属性以处理错误情况。

这是代码(方法A):

private List<Dto> _items;
public List<Dto> Items
{
    get => _items;
    set => this.RaiseAndSetIfChanged(ref _items, value);
}
RefreshCommand = ReactiveCommand.CreateFromObservable(() => GetItems
                    .Do(dtos => Items = dtos)
                    .Select(_ => Unit.Default))
                .DisposeWith(Disposables);
RefreshCommand.ThrownExceptions.Subscribe(err => Console.WriteLine(err.ToString()));
RefreshCommand.ExecuteIfPossible();

我还尝试了其他方法(方法 B):

private readonly ObservableAsPropertyHelper<List<Dto>> _items;
public List<Dto> Items => _items.Value;
this._items = GetItems.ToProperty(this, x => x.Items);

我的问题是:

  1. 如果我订阅了 `ThrownExceptions`,为什么方法 A 会抛出错误?请注意,永远不会命中 `Console.WriteLine(err.ToString())`。
  2. 为什么方法 B 在知道方法 A 失败的情况下工作正常?

我正在使用Uno.ReactiveUI,它是 reactiveUI 8.0.0 版的一个分支

谢谢你。

更新

如果我注释掉ExecuteIfPossible并点击RefreshCommand绑定的按钮,那么我没有运行时错误。当我离开ExecuteIfPossible时,内在的例外是

无法加载文件或程序集“Uno.Foundation,版本=255.255.255.255,文化=中性,PublicKeyToken=null”。该系统找不到指定的文件。

我什至不知道什么ExecuteIfPossible是诚实的。我只是想打电话Execute(null),但智能感知不会让我(也不会编译)。所以这肯定与 ReactiveUI 的 Uno 实现有关。

更新2

为了Execute可用,我必须使用RefreshCommandtype ReactiveCommand<Unit, Unit>。然后我可以编写以下内容,一切都按预期工作。

RefreshCommand.Execute(Unit.Default).Subscribe().DisposeWith(Disposables);

更新3

这个问题毫无意义,应该“关闭”,因为该问题与其标题或描述无关。只有我:

  1. 没有正确使用 ReactiveUI
  2. 调用一个抛出异常的 Uno.Client 方法并且错误地断定这是一个 ReactiveUI 问题,而实际上它不是。

感谢那些花时间帮助我的人。

标签: c#reactivereactiveui

解决方案


推荐阅读