Observable.FromEventPattern < TEventArgs > 方法 (物件,String)
使用反映來尋找實例事件,將符合標準 .NET 事件模式的 .NET 事件轉換為可觀察的序列。
Namespace:System.Reactive.Linq
裝配: System.Reactive.dll) 中的 System.Reactive (
Syntax
'Declaration
Public Shared Function FromEventPattern(Of TEventArgs As EventArgs) ( _
target As Object, _
eventName As String _
) As IObservable(Of EventPattern(Of TEventArgs))
'Usage
Dim target As Object
Dim eventName As String
Dim returnValue As IObservable(Of EventPattern(Of TEventArgs))
returnValue = Observable.FromEventPattern(target, _
eventName)
public static IObservable<EventPattern<TEventArgs>> FromEventPattern<TEventArgs>(
Object target,
string eventName
)
where TEventArgs : EventArgs
public:
generic<typename TEventArgs>
where TEventArgs : EventArgs
static IObservable<EventPattern<TEventArgs>^>^ FromEventPattern(
Object^ target,
String^ eventName
)
static member FromEventPattern :
target:Object *
eventName:string -> IObservable<EventPattern<'TEventArgs>> when 'TEventArgs : EventArgs
JScript does not support generic types and methods.
類型參數
- TEventArgs
事件的類型。
參數
- 目標
類型: System.Object
公開要轉換之事件的物件實例。
- eventName
類型: System.String
要轉換的事件名稱。
傳回值
類型:System.IObservable<EventPattern< TEventArgs>>
傳回值是可觀察的序列,其中包含基礎 .NET 事件調用的資料表示。
備註
FromEventPattern 運算子會將 .Net 事件轉換成EventPattern < TEventArgs >序列。 每個 EventPattern 實例都包含事件引數和傳送事件的物件。 事件引數是在序列中傳遞之每個 EventPattern 的 EventArgs 屬性中提供。 傳送事件的物件是在 EventPattern 實例的 Sender 屬性中提供。 所要的事件是藉由傳遞物件,將事件公開為 目標 參數,以及將 eventName 參數設定為事件的名稱來指定。 TEventArgs類型會指定每個事件將傳遞的事件引數類型。
範例
此範例程式碼示範如何使用 FromEventPattern 運算子接聽 System.IO.FileSystemWatcher 上的建立、重新命名和刪除事件。 此範例會監看 C:\Users\Public 資料夾中的變更,並將事件寫入主控台視窗。
using System;
using System.Reactive.Linq;
using System.Reactive;
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 FromEventPattern operator to setup a subscription to the Created event. ***//
//***************************************************************************************//
IObservable<EventPattern<FileSystemEventArgs>> fswCreated = Observable.FromEventPattern<FileSystemEventArgs>(fsw, "Created");
fswCreated.Subscribe(pattern => Console.WriteLine("{0} was created in {1}.", pattern.EventArgs.Name, ((FileSystemWatcher)pattern.Sender).Path));
//***************************************************************************************//
//*** Use the FromEventPattern operator to setup a subscription to the Renamed event. ***//
//***************************************************************************************//
IObservable<EventPattern<RenamedEventArgs>> fswRenamed = Observable.FromEventPattern<RenamedEventArgs>(fsw, "Renamed");
fswRenamed.Subscribe(pattern => Console.WriteLine("{0} was renamed to {1} in {2}.", pattern.EventArgs.OldName,
pattern.EventArgs.Name, ((FileSystemWatcher)pattern.Sender).Path));
//***************************************************************************************//
//*** Use the FromEventPattern operator to setup a subscription to the Deleted event. ***//
//***************************************************************************************//
IObservable<EventPattern<FileSystemEventArgs>> fswDeleted = Observable.FromEventPattern<FileSystemEventArgs>(fsw, "Deleted");
fswDeleted.Subscribe(pattern => Console.WriteLine("{0} was deleted in {1}.", pattern.EventArgs.Name, ((FileSystemWatcher)pattern.Sender).Path));
Console.WriteLine("Press ENTER to exit...\n");
Console.ReadLine();
}
}
}
下列輸出示範如何執行範例程式碼,以在 C:\Users\Public 目錄中建立新的文字檔。 檔案已重新命名為ExFile.txt,然後刪除。
Press ENTER to exit...
New Text Document.txt was created in C:\Users\Public.
New Text Document.txt was renamed to ExFile.txt in C:\Users\Public.
ExFile.txt was deleted in C:\Users\Public.