Método Observable.FromEvent TDelegate, TEventArgs> (Func<Action<TEventArgs>, TDelegate>, Action<TDelegate>, Action<TDelegate>)<
Converte um evento .NET em uma sequência observável.
Namespace:System.Reactive.Linq
Assembly: System.Reactive (em System.Reactive.dll)
Sintaxe
'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.
Parâmetros de tipo
- Tdelegate
O tipo de delegado.
- Teventargs
O tipo do evento.
Parâmetros
- conversão
Tipo: System.Func<Action<TEventArgs>, TDelegate>
Uma função usada para converter o manipulador de eventos fornecido em um delegado compatível com o evento .NET subjacente. O delegado resultante é usado em chamadas para os parâmetros de ação addHandler e removeHandler.
- Addhandler
Tipo: System.Action<TDelegate>
Ação que anexa o manipulador de eventos especificado ao evento .NET subjacente.
- Removehandler
Tipo: System.Action<TDelegate>
Ação que desanexa o manipulador de eventos especificado do evento .NET subjacente.
Valor Retornado
Tipo: System.IObservable<TEventArgs>
Sequência observável que contém representações de dados de invocações do evento .NET subjacente.
Comentários
O operador FromEvent cria uma sequência observável dos argumentos de evento fornecidos com os eventos subjacentes quando são acionados. O operador FromEvent só funciona com delegados do tipo Action<T>. Portanto, uma função de conversão deve ser usada para criar um manipulador de eventos compatível com o evento .NET subjacente. O código de exemplo neste tópico demonstra essa conversão para os delegados System.IO.FileSystemEventHandler e System.IO.RenamedEventHandler.
Exemplos
Este código de exemplo demonstra o uso do operador FromEvent para escutar eventos Criar, Renomear e Excluir em um System.IO.FileSystemWatcher. O exemplo observa esses eventos na pasta C:\Users\Public. O operador FromEvent só dá suporte a delegados do tipo Action<T>. Portanto, este exemplo usa o parâmetro de conversão para definir uma expressão lambda para converter o delegado system<T> para os delegados System.IO.FileSystemEventHandler e System.IO.RenamedEventHandler. Os eventos são observados usando uma assinatura para cada uma das sequências observáveis. Cada um dos eventos é gravado na janela do console.
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();
}
}
}
A saída a seguir foi gerada com o código de exemplo.
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.