gpt4 book ai didi

c# - 如何使用 ReactiveCommand?

转载 作者:行者123 更新时间:2023-12-03 08:25:35 31 4
gpt4 key购买 nike

我有一个使用 ReactiveCommand 的 View 模型:

public ReactiveCommand<Unit, Unit> Command { get; }

public MyViewModel()
{
Command = ReactiveCommand<Unit, Unit>.Create(MyMethod);
}

并将它绑定(bind)到一个按钮上:

<DataTemplate>
<Button Content="Press me"
Command="{Binding Command, Mode=OneTime}"
Margin="0,0,5,0"/>
</DataTemplate>

我像这样将 View 绑定(bind)到 View 模型(在我的情况下是最方便的方法):

myView.DataContext = new MyViewModel();

当我按下按钮时,MyMethod 被执行,并且在 MyMethod 中的最后一个运算符执行之后,我的应用程序崩溃了:

InvalidOperationException: The calling thread cannot access this object because a different thread owns it.

  StackTrace:
at System.Windows.Threading.Dispatcher.VerifyAccess()
at System.Windows.DependencyObject.GetValue(DependencyProperty dp)
at System.Windows.Controls.Primitives.ButtonBase.get_Command()
at System.Windows.Controls.Primitives.ButtonBase.UpdateCanExecute()
at System.Windows.Controls.Primitives.ButtonBase.OnCanExecuteChanged(Object sender, EventArgs e)
at System.Windows.Input.CanExecuteChangedEventManager.HandlerSink.OnCanExecuteChanged(Object sender, EventArgs e)
at ReactiveUI.ReactiveCommand.OnCanExecuteChanged()
at ReactiveUI.ReactiveCommand`2.<.ctor>b__9_6(Boolean _)
at System.Reactive.AnonymousSafeObserver`1.OnNext(T value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\AnonymousSafeObserver.cs:line 54
at System.Reactive.Linq.ObservableImpl.RefCount`1._.OnNext(TSource value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Linq\Observable\RefCount.cs:line 66
at System.Reactive.Subjects.FastImmediateObserver`1.EnsureActive(Int32 count) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Subjects\ReplaySubject.cs:line 893
at System.Reactive.Subjects.FastImmediateObserver`1.EnsureActive() in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Subjects\ReplaySubject.cs:line 753
at System.Reactive.Subjects.ReplaySubject`1.ReplayBase.OnNext(T value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Subjects\ReplaySubject.cs:line 276
at System.Reactive.Subjects.ReplaySubject`1.OnNext(T value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Subjects\ReplaySubject.cs:line 167
at System.Reactive.Linq.ObservableImpl.AsObservable`1._.OnNext(TSource value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Linq\Observable\AsObservable.cs:line 32
at System.Reactive.Linq.ObservableImpl.DistinctUntilChanged`2._.OnNext(TSource value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Linq\Observable\DistinctUntilChanged.cs:line 79
at System.Reactive.Linq.ObservableImpl.CombineLatest`3._.SecondObserver.OnNext(TSecond value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Linq\Observable\CombineLatest.cs:line 189
at System.Reactive.Linq.ObservableImpl.RefCount`1._.OnNext(TSource value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Linq\Observable\RefCount.cs:line 66
at System.Reactive.Subjects.FastImmediateObserver`1.EnsureActive(Int32 count) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Subjects\ReplaySubject.cs:line 893
at System.Reactive.Subjects.FastImmediateObserver`1.EnsureActive() in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Subjects\ReplaySubject.cs:line 753
at System.Reactive.Subjects.ReplaySubject`1.ReplayBase.OnNext(T value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Subjects\ReplaySubject.cs:line 276
at System.Reactive.Subjects.ReplaySubject`1.OnNext(T value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Subjects\ReplaySubject.cs:line 167
at System.Reactive.Linq.ObservableImpl.AsObservable`1._.OnNext(TSource value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Linq\Observable\AsObservable.cs:line 32
at System.Reactive.Linq.ObservableImpl.DistinctUntilChanged`2._.OnNext(TSource value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Linq\Observable\DistinctUntilChanged.cs:line 79
at System.Reactive.Linq.ObservableImpl.Concat`1._.OnNext(TSource value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Linq\Observable\Concat.cs:line 34
at System.Reactive.Linq.ObservableImpl.Select`2.Selector._.OnNext(TSource value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Linq\Observable\Select.cs:line 49
at System.Reactive.Linq.ObservableImpl.Scan`2._.OnNext(TSource value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Linq\Observable\Scan.cs:line 50
at System.Reactive.SafeObserver`1.OnNext(TSource value) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Internal\SafeObserver.cs:line 50
at System.Reactive.ScheduledObserver`1.Dispatch(ICancelable cancel) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Internal\ScheduledObserver.cs:line 93
at System.Reactive.Concurrency.Scheduler.<>c.<ScheduleLongRunning>b__73_0(Action`1 a, ICancelable c) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Concurrency\Scheduler.Simple.cs:line 78
at System.Reactive.Concurrency.DefaultScheduler.LongRunning.<>c__DisplayClass1_0`1.<ScheduleLongRunning>b__0(Object arg) in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Concurrency\DefaultScheduler.cs:line 165
at System.Reactive.Concurrency.ConcurrencyAbstractionLayerImpl.<>c__DisplayClass7_0.<StartThread>b__0() in D:\a\1\s\Rx.NET\Source\src\System.Reactive\Concurrency\ConcurrencyAbstractionLayerImpl.cs:line 56
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()

我使用的 ReactiveCommand 是错误的还是 ReactiveUI 中的错误?

我能够在仅使用空方法调用 ReactiveCommand 的最小 WPF 应用程序中重现此错误。

最佳答案

我只需要安装 ReactiveUI.WPF NuGet 包。

真的很想为 ReactiveUI 提供更好的文档。

关于c# - 如何使用 ReactiveCommand?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51747323/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com