共用方式為


BehaviorSubject < T > 建構函式

初始化BehaviorSubject < T >類別的新實例,這個類別會建立主旨來快取其最後一個值,並以指定的值開頭。

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

語法

'Declaration
Public Sub New ( _
    value As T _
)
'Usage
Dim value As T

Dim instance As New BehaviorSubject(value)
public BehaviorSubject(
    T value
)
public:
BehaviorSubject(
    T value
)
new : 
        value:'T -> BehaviorSubject
public function BehaviorSubject(
    value : T
)

參數

  • value
    類型: T
    當主體尚未收到其他值時,傳送給觀察者的初始值。

備註

BehaviorSubject 會緩衝它透過其 IObservable 介面發佈的最後一個專案。 如果尚未透過其 IObservable 介面發佈任何專案,則建構函式中提供的初始專案是目前緩衝的專案。 當對 BehaviorSubject 的 IObservable 介面進行訂閱時,發行的順序會以目前緩衝的專案開始。

一旦 IObserver 介面收到完成,就不會從 BehaviorSubject 緩衝或發佈任何專案。

範例

此範例示範 BehaviorSubject。 此範例會使用 Interval 運算子每秒將整數發佈至整數序列。 發行 10 個整數之後,Take 運算子將會完成序列。 這是 BehaviorSubject 訂閱的順序。

會為 BehaviorSubject 的 IObservable 介面建立兩個訂用帳戶,以顯示其發佈資料的方式。

  • 訂用帳戶#1:此訂用帳戶會從開頭開始,並顯示序列中建構函式的初始緩衝值 (-9) 。

  • 訂用帳戶#2:此訂用帳戶會在 5 秒睡眠後啟動。 此訂用帳戶會顯示序列開頭為目前緩衝的專案。

using System;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Reactive.Concurrency;
using System.Threading;

namespace Example
{
  class Program
  {
    static void Main()
    {
      //********************************************************************************************************//
      //*** A subject acts similar to a proxy in that it acts as both a subscriber and a publisher           ***//
      //*** It's IObserver interface can be used to subscribe to multiple streams or sequences of data.      ***//
      //*** The data is then published through it's IObservable interface.                                   ***//
      //***                                                                                                  ***//
      //*** A BehaviorSubject buffers the last item it published through its IObservable interface. If no    ***//
      //*** item has been published through its IObservable interface then the initial item provided in the  ***//
      //*** constructor is the current buffered item. When a subscription is made to the BehaviorSubject's   ***//
      //*** IObservable interface, the sequence published begins with the currently buffered item.           ***//
      //***                                                                                                  ***//
      //*** No items are buffered or published from a BehaviorSubject once its IObserver interface receives  ***//
      //*** a completion.                                                                                    ***//
      //***                                                                                                  ***//
      //*** In this example, we use the Interval operator to publish an integer to a integer sequence every  ***//
      //*** second. The sequence will be completed by the Take operator after 10 integers are published.     ***//
      //*** This will be the sequence that the BehaviorSubject subscribes to.                                ***//
      //***                                                                                                  ***//
      //*** We will create 2 subscriptions to the BehaviorSubject's IObservable interface to show how it     ***//
      //*** publishes it's data.                                                                             ***//
      //***                                                                                                  ***//
      //*** Subscription #1 : This subscription will start at the very beginning and will show the initial   ***//
      //***                   buffered value from the constructor (-9) in the sequence.                      ***//
      //***                                                                                                  ***//
      //*** Subscription #2 : This subscription will start after a 5 sec. sleep showing the sequence starts  ***//
      //***                   with the currently buffered item.                                              ***//
      //********************************************************************************************************//

      BehaviorSubject<long> myBehaviorSubject = new BehaviorSubject<long>((-9));
      Observable.Interval(TimeSpan.FromSeconds(1), Scheduler.ThreadPool).Take(10).Subscribe(myBehaviorSubject);

      
      //********************************************************************************************************//
      //*** Subscription #1 : This subscription will start at the very beginning and will show the initial   ***//
      //***                   buffered value from the constructor (-9) in the sequence.                      ***//
      //********************************************************************************************************//

      EventWaitHandle wait1 = new EventWaitHandle(false, EventResetMode.ManualReset);
      myBehaviorSubject.Subscribe(x => Console.WriteLine("Subscription #1 observes : " + x),
                                  () => 
                                  {
                                    Console.WriteLine("Subscription #1 completed.");
                                    wait1.Set();
                                  });


      //********************************************************************************************************//
      //*** Subscription #2 : This subscription will start after a 5 sec. sleep showing the sequence starts  ***//
      //***                   with the currently buffered item.                                              ***//
      //********************************************************************************************************//
    
      Thread.Sleep(5000);
      EventWaitHandle wait2 = new EventWaitHandle(false, EventResetMode.ManualReset);
      myBehaviorSubject.Subscribe(x => Console.WriteLine("{0,30}Subscription #2 observes : {1}", " ", x), 
                                  () => 
                                  {
                                    Console.WriteLine("{0,30}Subscription #2 completed.", " ");
                                    wait2.Set();
                                  });


      //**************************************************//
      // *** Wait for completion on both subscriptions ***//
      //**************************************************//

      WaitHandle.WaitAll(new WaitHandle[] { wait1, wait2 });
      myBehaviorSubject.Dispose();

      Console.WriteLine("\nPress ENTER to exit...");
      Console.ReadLine();
    }
  }
}

範例程式碼的下列輸出會顯示重迭的訂用帳戶。

Subscription #1 observes : -9
Subscription #1 observes : 0
Subscription #1 observes : 1
Subscription #1 observes : 2
Subscription #1 observes : 3
Subscription #1 observes : 4
                              Subscription #2 observes : 4
Subscription #1 observes : 5
                              Subscription #2 observes : 5
Subscription #1 observes : 6
                              Subscription #2 observes : 6
Subscription #1 observes : 7
                              Subscription #2 observes : 7
Subscription #1 observes : 8
                              Subscription #2 observes : 8
Subscription #1 observes : 9
                              Subscription #2 observes : 9
Subscription #1 completed.
                              Subscription #2 completed.

Press ENTER to exit...

另請參閱

參考

BehaviorSubject < T > 類別

System.Reactive.Subjects 命名空間