将 .NET 事件转换为可观测序列。
              Namespace:System.Reactive.Linq
              装配: System.Reactive.dll) 中的 System.Reactive (
语法
'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.