Observable.Window<TSource> 方法 (IObservable<TSource>、Int32、Int32)

将可观测序列的每个元素投影到零个或多个窗口,这些窗口基于元素计数信息生成。

Namespace:System.Reactive.Linq
装配: System.Reactive.dll) 中的 System.Reactive (

语法

'Declaration
<ExtensionAttribute> _
Public Shared Function Window(Of TSource) ( _
    source As IObservable(Of TSource), _
    count As Integer, _
    skip As Integer _
) As IObservable(Of IObservable(Of TSource))
'Usage
Dim source As IObservable(Of TSource)
Dim count As Integer
Dim skip As Integer
Dim returnValue As IObservable(Of IObservable(Of TSource))

returnValue = source.Window(count, _
    skip)
public static IObservable<IObservable<TSource>> Window<TSource>(
    this IObservable<TSource> source,
    int count,
    int skip
)
[ExtensionAttribute]
public:
generic<typename TSource>
static IObservable<IObservable<TSource>^>^ Window(
    IObservable<TSource>^ source, 
    int count, 
    int skip
)
static member Window : 
        source:IObservable<'TSource> * 
        count:int * 
        skip:int -> IObservable<IObservable<'TSource>> 
JScript does not support generic types and methods.

类型参数

  • TSource
    源的类型。

参数

  • skip
    类型: System.Int32
    在创建连续窗口之间要跳过的元素数。

返回值

类型: System.IObservable<IObservable<TSource>>
可观测的窗口序列。

使用说明

在 Visual Basic 和 C# 中,可以在 IObservable<TSource> 类型的任何对象上调用此方法作为实例方法。 当使用实例方法语法调用此方法时,请省略第一个参数。 有关详细信息,请参阅

备注

Window 运算符可用于将序列分解为缓冲子集,例如序列的开窗视图。 count 参数控制每个窗口中放置的项数。 skip 参数通过对main序列中生成的项进行计数来控制下一个窗口何时开始。 当跳过指定数量的项时,新窗口将开始缓冲序列的子集。

示例

此示例使用 Interval 运算符生成main整数序列。 每秒生成一个新整数。 Window 运算符将main序列分解为子集,如整数序列的开窗视图。 每个窗口将包含序列中从第一个项 (0) 开始的三个项的计数。 然后跳过五个项,以便我们实现包含三个整数的整数序列的窗口。 每个窗口以从 0 开始的第 5 个整数开始。

using System;
using System.Reactive.Linq;
using System.Reactive.Concurrency;

namespace Example
{
  class Program
  {
    static void Main()
    {
      //***********************************************************************************************//
      //*** The mainSequence produces a new long integer from the Interval operator every second    ***//
      //*** but this sequence is broken up by the Window operator into subsets like a windowed      ***//
      //*** view of the sequence. The count parameter controls how many items are placed in each    ***//
      //*** window. The skip parameter controls when the next window starts by counting the items   ***//
      //*** produced in the main sequence. When the the specified number of items are skipped, a    ***//
      //*** new window starts.                                                                      ***//
      //***                                                                                         ***//
      //*** In this example each window will contain a count of 3 items from the sequence starting  ***//
      //*** with the first item (0). 5 items are "skipped" to determine when the next window opens. ***//
      //*** So the result is that the integer sequences in the windows start with every 5th integer ***//
      //*** beginning at 0 and include 3 integers.                                                  ***//
      //***********************************************************************************************//

      var mainSequence = Observable.Interval(TimeSpan.FromSeconds(1));

      int count = 3;
      int skip = 5;
      var seqWindowed = mainSequence.Window(count, skip);


      //*********************************************************************************************//
      //*** A subscription to seqWindowed will provide a new IObservable<long> for every 5th item ***//
      //*** in the main sequence starting with the first item.                                    ***//
      //***                                                                                       ***//
      //*** Create a subscription to each window into the main sequence and list the value.       ***//
      //*********************************************************************************************//

      Console.WriteLine("\nCreating the subscription. Press ENTER to exit...\n");
      seqWindowed.Subscribe(seqWindow =>
      {
        Console.WriteLine("\nA new window into the main sequence has been opened\n");

        seqWindow.Subscribe(x =>
        {
          Console.WriteLine("Integer : {0}", x);
        });
      });

      Console.ReadLine();
    }
  }
}

以下输出是使用示例代码生成的。

Creating the subscription. Press ENTER to exit...


A new window into the main sequence has been opened

Integer : 0
Integer : 1
Integer : 2

A new window into the main sequence has been opened

Integer : 5
Integer : 6
Integer : 7

A new window into the main sequence has been opened

Integer : 10
Integer : 11
Integer : 12

A new window into the main sequence has been opened

Integer : 15
Integer : 16
Integer : 17

另请参阅

参考

可观测类

窗口重载

System.Reactive.Linq 命名空间