C# .NET Rx- System.Reactive 在哪里?

C# .NET Rx- Where is System.Reactive?

我有丰富的 Java 背景,所以如果我忽略了 C# 中一些明显的东西,请原谅我,但我的研究让我无处可去。我正在尝试使用反应式 Rx .NET 库。编译器并没有抱怨 IObservable 但它是对 zip 方法的调用。它抛出“...您是否缺少 using 指令或程序集引用?”

我一直在浏览命名空间,但找不到要查找的内容。我找不到 System.Reactive,如果使用它也会引发错误,并且此 Windows 8.1 应用程序已包含所有参考。有人可以告诉我哪里出了问题吗?

public sealed class EventEngine
{    
    private static readonly EventEngine singleton = new EventEngine();

    public static EventEngine get()
    {
        return singleton;
    }

    public IObservable<MusicNote> CurrentKey { get; set; }
    public IObservable<Scale> CurrentScale { get; set; }

    public IObservable<AppliedScale> CurrentAppliedScale
    {
        get
        {
            return CurrentScale.zip(CurrentKey,
                (s, k) => AppliedScale.getAppliedScale(k, s));
        } 
    }

    private EventEngine() {}
}

*更新*

这是考虑了答案输入后的工作版本。

public sealed class EventEngine
{
    private static readonly EventEngine singleton = new EventEngine();

    public static EventEngine get()
    {
        return singleton;
    }

    public IObservable<MusicNote> CurrentKey { get; set; }
    public IObservable<Scale> CurrentScale { get; set; }

    public IObservable<AppliedScale> CurrentAppliedScale
    {
        get
        {
            return Observable.Zip(CurrentScale, CurrentKey,
                (s, k) => AppliedScale.getAppliedScale(s,k));
        } 
    }

    private EventEngine() {}
}

您可能没有将必要的 Rx 程序集引用添加到您的项目。 (引用程序集与导入命名空间不是一回事!你已经知道什么是命名空间;程序集类似于JAR;最小的代码单元deployment/distribution。您的项目必须在其中定义的命名空间可供使用之前引用它。)

编译器可能不会抱怨 IObservable<T>IObserver<T>,因为您的项目面向 .NET Framework 版本 4 或更高版本。自 .NET 版本 4 以来,这两个接口 一直是核心 .NET Framework Class 库 (FCL) 的一部分。(如果您的目标是更早的 .NET 版本,您会使用这些未定义的接口也会出错。)

除了这两个接口之外,Rx 的每个部分包含在核心 .NET FCL 中,而是驻留在它们自己的(附加)程序集中。您可以将它们添加到您的项目中,例如通过安装相应的 NuGet packages:

  1. 在 Visual Studio 中,转到 工具NuGet 包管理器包管理器控制台.

  2. 在NuGet控制台window,select下拉列表中的目标项目(你想使用Rx的地方)默认项目.

  3. 接下来,输入 Install-Package System.Reactive 并点击 Enter ↵。 (注意:此包以前称为 Rx-Main;详情请参阅 。)

  4. 这会将 System.Reactive.* 程序集引用添加到您的项目。

从版本 3 开始,Rx 项目现在称为 System.Reactive,因为它 "brings the NuGet package naming in line with NuGet guidelines and also the dominant namespace in each package."

可以通过搜索 "System.Reactive" 从 NuGet 下载,也可以在这里下载:https://www.nuget.org/packages/System.Reactive/

这是项目的 GitHub 页面:https://github.com/Reactive-Extensions/Rx.NET

包映射

  • Rx-Main System.Reactive
  • Rx-Core System.Reactive.Core
  • Rx-Interfaces System.Reactive.Interfaces
  • Rx-Linq System.Reactive.Linq
  • Rx-PlatformServices System.Reactive.PlatformServices
  • Rx-Testing Microsoft.Reactive.Testing

Source