共用方式為


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

將可觀察序列的每個元素投影成零個或多個根據專案計數資訊產生的視窗。

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

Syntax

'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 參數會控制下一個視窗的開始時間,方法是計算主要序列中產生的專案。 當略過指定的專案數目時,新的視窗會開始緩衝序列的子集。

範例

此範例會使用 Interval 運算子產生整數的主要序列。 每秒會產生一個新的整數。 主要序列是由 Window 運算子細分成子集,例如整數序列的視窗檢視。 每個視窗都會包含序列中從第一個專案開始的三個專案計數, (0) 。 然後會略過五個專案,讓我們達到包含三個整數的整數序列中的視窗。 每個視窗會從第 5 個整數開始,從 0 開始。

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

另請參閱

參考

Observable 類別

視窗多載

System.Reactive.Linq 命名空間