共用方式為


Observable.FromEvent TDelegate, TEventArgs < 方法 (Func < Action < TEventArgs >> , TDelegate > , Action TDelegate, Action << TDelegate >>)

將 .NET 事件轉換為可觀察的序列。

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

Syntax

'Declaration
Public Shared Function FromEvent(Of TDelegate, TEventArgs) ( _
    conversion As Func(Of Action(Of TEventArgs), TDelegate), _
    addHandler As Action(Of TDelegate), _
    removeHandler As Action(Of TDelegate) _
) As IObservable(Of TEventArgs)
'Usage
Dim conversion As Func(Of Action(Of TEventArgs), TDelegate)
Dim addHandler As Action(Of TDelegate)
Dim removeHandler As Action(Of TDelegate)
Dim returnValue As IObservable(Of TEventArgs)

returnValue = Observable.FromEvent(conversion, _
    addHandler, removeHandler)
public static IObservable<TEventArgs> FromEvent<TDelegate, TEventArgs>(
    Func<Action<TEventArgs>, TDelegate> conversion,
    Action<TDelegate> addHandler,
    Action<TDelegate> removeHandler
)
public:
generic<typename TDelegate, typename TEventArgs>
static IObservable<TEventArgs>^ FromEvent(
    Func<Action<TEventArgs>^, TDelegate>^ conversion, 
    Action<TDelegate>^ addHandler, 
    Action<TDelegate>^ removeHandler
)
static member FromEvent : 
        conversion:Func<Action<'TEventArgs>, 'TDelegate> * 
        addHandler:Action<'TDelegate> * 
        removeHandler:Action<'TDelegate> -> IObservable<'TEventArgs> 
JScript does not support generic types and methods.

類型參數

  • TDelegate
    委派的類型。
  • TEventArgs
    事件的類型。

參數

  • 轉換
    類型:System.Func<Action< TEventArgs > 、TDelegate>
    函式,用來將指定的事件處理常式轉換為與基礎 .NET 事件相容的委派。 產生的委派用於對 addHandler 和 removeHandler 動作參數的呼叫。
  • addHandler
    類型:System.Action< TDelegate>
    將指定事件處理常式附加至基礎 .NET 事件的動作。
  • removeHandler
    類型:System.Action< TDelegate>
    從基礎 .NET 事件中斷連結指定事件處理常式的動作。

傳回值

類型:System.IObservable< TEventArgs>
可觀察序列,其中包含基礎 .NET 事件調用的資料表示。

備註

FromEvent 運算子會在引發事件時,建立與基礎事件一起提供之事件引數的可觀察序列。 FromEvent 運算子只適用于 Action < T > 類型的委派。 因此,轉換函式必須用來建立與基礎 .NET 事件相容的事件處理常式。 本主題中的範例程式碼示範 System.IO.FileSystemEventHandler 和 System.IO.RenamedEventHandler 委派的這個轉換。

範例

此範例程式碼示範如何使用 FromEvent 運算子接聽 System.IO.FileSystemWatcher 上的建立、重新命名和刪除事件。 此範例會在 C:\Users\Public 資料夾中監看這些事件。 FromEvent 運算子只支援 Action < T > 類型的委派。 因此,此範例會使用轉換參數來定義 Lambda 運算式,將 System T > 委派轉換成 System.IO.FileSystemEventHandler < 和 System.IO.RenamedEventHandler 委派。 事件是使用每個可觀察序列的訂用帳戶來觀察。 每個事件都會寫入主控台視窗。

using System;
using System.Reactive.Linq;
using System.IO;

namespace Example
{
  class Program
  {
    static void Main()
    {
      //*********************************************************************************************************************//
      //*** Create a FileSystemWatcher to watch the C:\Users\Public directory using the default NotifyFilter watching for ***//
      //*** changes to any type of file.                                                                                  ***//
      //*********************************************************************************************************************//

      FileSystemWatcher fsw = new FileSystemWatcher(@"C:\Users\Public", "*.*");
      fsw.EnableRaisingEvents = true;


      //******************************************************************************************//
      //*** Use the FromEvent operator to setup a subscription to the Created event.           ***//
      //***                                                                                    ***//
      //*** The first lambda expression performs the conversion of Action<FileSystemEventArgs> ***//
      //*** to FileSystemEventHandler. The FileSystemEventHandler just calls the handler       ***//
      //*** passing the FileSystemEventArgs.                                                   ***//
      //***                                                                                    ***//
      //*** The other lambda expressions add and remove the FileSystemEventHandler to and from ***//
      //*** the event.                                                                         ***//
      //******************************************************************************************//

      var fswCreated = Observable.FromEvent<FileSystemEventHandler, FileSystemEventArgs>(handler => 
                                  {
                                    FileSystemEventHandler fsHandler = (sender, e) =>
                                    {
                                      handler(e); 
                                    };

                                    return fsHandler; 
                                  },
                                  fsHandler => fsw.Created += fsHandler,
                                  fsHandler => fsw.Created -= fsHandler);

fswCreated.Subscribe(e => Console.WriteLine("{0} was created.", e.FullPath));


      //******************************************************************************************//
      //*** Use the FromEvent operator to setup a subscription to the Renamed event.           ***//
      //***                                                                                    ***//
      //*** The first lambda expression performs the conversion of Action<RenamedEventArgs>    ***//
      //*** to RenamedEventHandler. The RenamedEventHandler just calls the handler passing the ***//
      //*** RenamedEventArgs.                                                                  ***//
      //***                                                                                    ***//
      //*** The other lambda expressions add and remove the RenamedEventHandler to and from    ***//
      //*** the event.                                                                         ***//
      //******************************************************************************************//

      var fswRenamed = Observable.FromEvent<RenamedEventHandler, RenamedEventArgs>(handler => 
                                  {
                                    RenamedEventHandler fsHandler = (sender, e) =>
                                    {
                                      handler(e); 
                                    };
      
                                    return fsHandler; 
                                  },
                                  fsHandler => fsw.Renamed += fsHandler,
                                  fsHandler => fsw.Renamed -= fsHandler);

      fswRenamed.Subscribe(e => Console.WriteLine("{0} was renamed to {1}.", e.OldFullPath, e.FullPath));


      //******************************************************************************************//
      //*** Use the FromEvent operator to setup a subscription to the Deleted event.           ***//
      //***                                                                                    ***//
      //*** The first lambda expression performs the conversion of Action<FileSystemEventArgs> ***//
      //*** to FileSystemEventHandler. The FileSystemEventHandler just calls the handler       ***//
      //*** passing the FileSystemEventArgs.                                                   ***//
      //***                                                                                    ***//
      //*** The other lambda expressions add and remove the FileSystemEventHandler to and from ***//
      //*** the event.                                                                         ***//
      //******************************************************************************************//

      var fswDeleted = Observable.FromEvent<FileSystemEventHandler, FileSystemEventArgs>(handler =>
                                  {
                                    FileSystemEventHandler fsHandler = (sender, e) =>
                                    {
                                      handler(e);
                                    };
      
                                    return fsHandler;
                                  },
                                  fsHandler => fsw.Deleted += fsHandler,
                                  fsHandler => fsw.Deleted -= fsHandler);

      fswDeleted.Subscribe(e => Console.WriteLine("{0} was deleted.", e.FullPath));
                

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

下列輸出是使用範例程式碼所產生。

Press ENTER to exit...

C:\Users\Public\New Text Document.txt was created.
C:\Users\Public\New Text Document.txt was renamed to C:\Users\Public\TestFile.txt.
C:\Users\Public\TestFile.txt was deleted.

另請參閱

參考

Observable 類別

FromEvent 多載

System.Reactive.Linq 命名空間