【问题标题】:How to use ReactiveUI with WinForms如何在 WinForms 中使用 ReactiveUI
【发布时间】:2016-03-22 08:41:43
【问题描述】:

在看到 ReactiveUI 可以做什么之后,我决定学习它,但是在第一次尝试运行一个简单的项目时,我的热情就被打破了。我使用来自 NuGet 的 reactiveui-winforms.Net40 版本 6.5.0 重新创建了 this 文章中的示例。一切编译正常,但我在运行时在以下行遇到异常

var OKCmdObs = this.WhenAny(vm => vm.EnteredText,
            s => !string.IsNullOrWhiteSpace(s.Value));



System.InvalidOperationException occurred
  HResult=-2146233079
  Message=The current thread has no Dispatcher associated with it.
  Source=System.Reactive.Windows.Threading
  StackTrace:
       at System.Reactive.Concurrency.DispatcherScheduler.get_Current()
       at ReactiveUI.PlatformRegistrations.<>c.<Register>b__0_7() in C:\workspace\git-perso\ReactiveUI\ReactiveUI\Platform\Registrations.cs:line 75
  InnerException: 

有人知道发生了什么吗? 提到的文章没有可供下载的编译项目,我也没有找到任何完整的用于响应式 Winforms 的“Hello-World”项目。


我的测试项目可以下载here


在 Visual Studio 中,如果我继续(F5),则会发生另一个异常:

System.NullReferenceException occurred
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=ReactiveUI
  StackTrace:
       at ReactiveUI.IROObservableForProperty.<>c__DisplayClass1_0.<GetNotificationForProperty>b__6(IReactivePropertyChangedEventArgs`1 x) in C:\workspace\git-perso\ReactiveUI\ReactiveUI\IROObservableForProperty.cs:line 44
  InnerException: 

如果我继续按 F5,我会得到:

    System.Exception was unhandled by user code
  HResult=-2146233088
  Message=An OnError occurred on an object (usually ObservableAsPropertyHelper) that would break a binding or command. To prevent this, Subscribe to the ThrownExceptions property of your objects
  Source=ReactiveUI

【问题讨论】:

    标签: winforms visual-studio-2010 c#-4.0 reactiveui


    【解决方案1】:

    这个异常是因为 RxUI 总是尝试为 WPF 初始化,即使(因为你也在使用 winforms 包)它会立即用基于 Winforms 的调度程序覆盖这个设置。

    但它应该是无害的,因为它会被捕获并忽略。您可能在 VS 中遇到了它?

    【讨论】:

    • 是的,它发生在 VS 中。我认为第一个异常确实被捕获(由谁?),但进一步我得到另一个异常;查看我更新的问题。
    • 无法重现,您的项目在初始异常后运行良好,也许您更改了什么?
    • 这很奇怪。我已经在另一台计算机上测试了我上传的项目(运行 W7 而不是 W8.1),我得到了相同的结果。此外,编译后的应用程序在所有经过测试的 SO(W8.1、W7、WinXp)上都会提供 NullReferenceException
    猜你喜欢
    • 1970-01-01
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-17
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多