C# 101 接收示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1772756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:41:55  来源:igfitidea点击:

101 Rx Examples

c#.netsystem.reactivereactive-programming

提问by Francisco Noriega

EDIT: Thanks for the link to the wiki, I think that since its already started there, its easier to go there to check it out. However the question here is also good, so people who are not around the msdn forums get to know about the wiki and where it is.

编辑:感谢您提供到 wiki 的链接,我认为因为它已经在那里开始,所以去那里检查更容易。然而,这里的问题也很好,所以不在 msdn 论坛周围的人可以了解 wiki 及其位置。

Short Question:

简短问题:

Do you have a Sample of Rx Code that could help people understand it better?

您是否有可以帮助人们更好地理解它的 Rx 代码示例?

Long rambling with hidden question:

漫无边际的隐藏问题:

Now that the Rx frameworkhas been released, I think that many of us are interested in getting the bits and trying them out. Sadly there really aren't many examples out there (after an exhaustive search I'm almost convinced Rx was meant only to do easy Drag on wpf apps).

现在Rx 框架已经发布,我想我们中的许多人都对获取这些内容并尝试使用它们感兴趣。可悲的是,那里真的没有很多例子(经过详尽的搜索,我几乎确信 Rx 只是为了在 wpf 应用程序上做简单的拖动)。

I can't recall exactly where it was that I read or heard (I've been looking at many blogs and videos) that the Rx team seems to be interested in doing the 101 series...when they get enough time to do it... which pretty much sucks for those who want to understand it and play with it now (and I mean, what self-respected developer doesn't feel like a kid with a new toy when a new tech. like this comes up).

我不记得我在哪里读到或听到过(我一直在查看许多博客和视频)Rx 团队似乎对制作 101 系列感兴趣……当他们有足够的时间去做时......对于那些现在想要理解它并玩它的人来说,这非常糟糕(我的意思是,当新技术出现时,有自尊的开发人员不会觉得自己像一个拿着新玩具的孩子。像这样) .

I've personally been giving a try now, but wow there are some crazy concepts in it... just to have methods names like Materializeand Zipmakes me think of Teleportersand stuff from Back to the Future.

我个人现在一直在尝试,但是哇,里面有一些疯狂的概念......只是像MaterializeZip这样的方法名称让我想起了Teleporters和来自Back to the Future 的东西。

So, I think it would be nice if the ones with greater understanding, helped to build a collection of examples, ala 101 Linq Examplesthat goes from basic usage to more complex stuff, and pretty much cover all of the methods and their use, in a practical way (perhaps with a little bit of theory too, specially since these kind of concepts probably required it)

因此,我认为如果有更深入理解的人帮助构建示例集合,ala 101 Linq 示例从基本用法到更复杂的内容,并且几乎涵盖所有方法及其用法,那就太好了一种实用的方法(也许也需要一点理论,特别是因为这些概念可能需要它)

I think its great that MS devs take time to give us material like that, but I also think this community is good enough to start building our own material, dont you?

我认为 MS 开发人员花时间为我们提供这样的材料很棒,但我也认为这个社区足以开始构建我们自己的材料,不是吗?

采纳答案by Robert Hencke

I actually had similar thoughts a couple days ago. We started our own "101 Rx Samples" as a post in the Rx MSDN forum, but we have since moved it to a Wiki format. Please feel free to come over and add your own samples!

其实前几天我也有类似的想法。我们开始了我们自己的“101 Rx 示例”作为 Rx MSDN 论坛中的一个帖子,但此后我们将其移至 Wiki 格式。请随时过来添加您自己的样品!

101 Rx Samples on the Rx wiki

Rx wiki 上的 101 个 Rx 示例

回答by amazedsaint

To start with - Here is a simple drawing application, so that when the user drags, we draw a red line from the initial mouse down position to the current location, and also a blue spot at the current location. This is the result of my last week's hack on Rx

首先 - 这是一个简单的绘图应用程序,因此当用户拖动时,我们会从鼠标初始位置到当前位置绘制一条红线,并在当前位置绘制一条蓝点。这是我上周对 Rx 的 hack 的结果

A WPF Drawing Demo

一个 WPF 绘图演示

And here is the source code.

这是源代码。

//A draw on drag method to perform the draw
void DrawOnDrag(Canvas e)
        {

            //Get the initial position and dragged points using LINQ to Events
            var mouseDragPoints = from md in e.GetMouseDown()
                                  let startpos=md.EventArgs.GetPosition(e)
                                  from mm in e.GetMouseMove().Until(e.GetMouseUp())
                                  select new
                                  {
                                      StartPos = startpos,
                                      CurrentPos = mm.EventArgs.GetPosition(e),
                                  };


            //Subscribe and draw a line from start position to current position
            mouseDragPoints.Subscribe
                (item =>
                {
                    e.Children.Add(new Line()
                    {
                        Stroke = Brushes.Red,
                        X1 = item.StartPos.X,
                        X2 = item.CurrentPos.X,
                        Y1 = item.StartPos.Y,
                        Y2 = item.CurrentPos.Y
                    });

                    var ellipse = new Ellipse()
                    {
                        Stroke = Brushes.Blue,
                        StrokeThickness = 10,
                        Fill = Brushes.Blue
                    };
                    Canvas.SetLeft(ellipse, item.CurrentPos.X);
                    Canvas.SetTop(ellipse, item.CurrentPos.Y);
                    e.Children.Add(ellipse);
                }
                );
        }

Read my post with further explanation hereand Download the source code here

在此处阅读我的帖子并进一步解释此处下载源代码

Hope this helps

希望这可以帮助

回答by Richard Anthony Hein

Here's my variation on the drag & drop sample by Wes Dyer, for Windows Forms (I'd make EnableDragging an extension method, probably):

这是我对Wes Dyer 的拖放示例的变体,用于 Windows 窗体(我可能将 EnableDragging 设为扩展方法):

    public Form2()
    {
        InitializeComponent();

        EnableDragging(pictureBox1);
        EnableDragging(button1);
        EnableDragging(this);
    }

    private void EnableDragging(Control c)
    {
        // Long way, but strongly typed.
        var downs = from down in Observable.FromEvent<MouseEventHandler, MouseEventArgs>(
                        eh => new MouseEventHandler(eh), 
                        eh => c.MouseDown += eh,  
                        eh => c.MouseDown -= eh)
                    select new { down.EventArgs.X, down.EventArgs.Y };

        // Short way.
        var moves = from move in Observable.FromEvent<MouseEventArgs>(c, "MouseMove")
                    select new { move.EventArgs.X, move.EventArgs.Y };

        var ups = Observable.FromEvent<MouseEventArgs>(c, "MouseUp");

        var drags = from down in downs
                    from move in moves.TakeUntil(ups)
                    select new Point { X = move.X - down.X, Y = move.Y - down.Y };

        drags.Subscribe(drag => c.SetBounds(c.Location.X + drag.X, c.Location.Y + drag.Y, 0, 0, BoundsSpecified.Location));
    }  

回答by avandeursen

Another useful resource may be the Reactive Extensions (Rx) Koans: 55 progressive examples to help you learn Rx

另一个有用的资源可能是Reactive Extensions (Rx) Koans55 个帮助您学习 Rx 的渐进示例

回答by Luciano

I'm reading http://www.introtorx.com, which like the name suggests appears to be a concise introduction. There appear to be quite a lot of (very basic) examples, step-by-step, mostly using the console to print stuff out.

我正在阅读http://www.introtorx.com,顾名思义,它似乎是一个简洁的介绍。似乎有很多(非常基本的)示例,一步一步,主要是使用控制台打印出来的东西。

回答by Xiaoguo Ge

And a Stock Viewerexample on Github enter image description here

以及Github 上的Stock Viewer示例 在此处输入图片说明

  1. StreamProvider pulls data from a server and generates a Rx.NET IObservable stream.
  2. StreamAggregator aggregates all IObservable streams and duplicates the result into a central processing thread.
  3. The Views filter the single stream and duplicate the result into their own threads for display.
  1. StreamProvider 从服务器拉取数据并生成 Rx.NET IObservable 流。
  2. StreamAggregator 聚合所有 IObservable 流并将结果复制到中央处理线程中。
  3. 视图过滤单个流并将结果复制到它们自己的线程中进行显示。

All StreamProviders, StreamAggregate, and Views run in their own threads. This is also a typical threading model of real-world stock viewing applications.

所有 StreamProviders、StreamAggregate 和 Views 都在它们自己的线程中运行。这也是现实世界股票查看应用程序的典型线程模型。

This example can also be a simple performance test skeleton for WPF DataGrid. It calculates ticks/second processed and displays it on the View.

此示例也可以是 WPF DataGrid 的简单性能测试框架。它计算每秒处理的滴答数并将其显示在视图上。

回答by Costin

A bit late, but if somebody new stumbles upon this question, http://rxmarbles.com/provides a very nice way to visualise the operators.

有点晚了,但如果有人偶然发现了这个问题,http://rxmarbles.com/提供了一种非常好的方式来可视化运算符。