C#.NET Rx- System.Reactive在哪里? [英] C# .NET Rx- Where is System.Reactive?

查看:92
本文介绍了C#.NET Rx- System.Reactive在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有丰富的Java背景,因此,如果我忽略了C#中显而易见的内容,请原谅我,但我的研究无济于事.我正在尝试使用反应性Rx .NET库.编译器不会抱怨 IObservable ,但是会调用 zip 方法.它抛出"...您是否缺少using指令或程序集引用?"

I have an intensive Java background so forgive me if I'm overlooking something obvious in C#, but my research is getting me nowhere. I am trying to use the reactive Rx .NET library. The compiler is not complaining about the IObservable but it is with the call to the zip method. It is throwing the "... are you missing a using directive or assembly reference?"

我已经遍历了命名空间,但找不到所需的内容.我找不到 System.Reactive ,如果使用它也会引发错误,并且此Windows 8.1应用程序已包含所有引用.有人可以给我带头解决什么问题吗?

I've been going through the namespaces and I cannot find what is looking for. I cannot find the System.Reactive which also throws an error if used, and all the references are already included for this Windows 8.1 app. Can someone please give me a lead on what is wrong?

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() {}
}

* UPDATE *

这里是考虑到答案输入后的工作版本.

Here is the working version after considering input from answers.

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;最小的代码部署/分发单元.您的项目必须在其中定义的名称空间可以使用之前,请对其进行引用.)

You have probably not added the necessary assembly references for Rx to your project. (Referencing an assembly is not the same thing as importing a namespace! You already know what a namespace is; an assembly is something similar to a JAR; the smallest unit of code deployment/distribution. Your project must reference it before the namespaces defined inside it become available for use.)

编译器可能不会抱怨 IObservable< T> IObserver< T> ,因为您的项目面向的是.NET Framework版本4或更高版本.从.NET版本4开始,这两个接口已经成为核心.NET Framework 类库(FCL)的一部分.(如果定位的是较早的.NET版本,则在使用这些未定义的接口时会出错.也是如此.)

The compiler likely doesn't complain about IObservable<T> and IObserver<T> because your project is targeting .NET Framework version 4 or later. These two interfaces have been part of the core .NET Framework Class Library (FCL) since .NET version 4. (If you targeted an earlier .NET version, you'd get errors for using these undefined interfaces, too.)

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

Every part of Rx other than these two interfaces is not included in the core .NET FCL, but resides in their own (add-on) assemblies. You can add them to your project e.g. by installing the corresponding NuGet packages:

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

  1. In Visual Studio, go to ToolsNuGet Package ManagerPackage Manager Console.

在NuGet控制台窗口中,从下拉列表默认项目中选择目标项目(要在其中使用Rx).

In the NuGet console window, select the target project (where you want to use Rx) in the drop-down list Default project.

下一步,键入 Install-Package System.Reactive ,然后按输入↵.(注意:此程序包以前曾被称为 Rx-Main ;有关详细信息,请参见此答案.)

Next, type Install-Package System.Reactive and hit Enter ↵. (Note: This package used to be called Rx-Main previously; see this answer for details.)

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

This will add the System.Reactive.* assembly references to your project.

这篇关于C#.NET Rx- System.Reactive在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆