UploadDataCompletedEventHandler Delegat

Definicja

Reprezentuje metodę, która obsłuży UploadDataCompleted zdarzenie klasy WebClient.

public delegate void UploadDataCompletedEventHandler(System::Object ^ sender, UploadDataCompletedEventArgs ^ e);
public delegate void UploadDataCompletedEventHandler(object sender, UploadDataCompletedEventArgs e);
type UploadDataCompletedEventHandler = delegate of obj * UploadDataCompletedEventArgs -> unit
Public Delegate Sub UploadDataCompletedEventHandler(sender As Object, e As UploadDataCompletedEventArgs)

Parametry

sender
Object

Źródło zdarzenia.

Przykłady

Poniższy przykład kodu pokazuje asynchronicznie przekazywanie danych.

void UploadDataInBackground2( String^ address )
{
   WebClient^ client = gcnew WebClient;
   Uri ^uri = gcnew Uri(address);
   String^ text = "Time = 12:00am temperature = 50";
   array<Byte>^data = System::Text::Encoding::UTF8->GetBytes( text );
   String^ method = "POST";

   client->UploadDataCompleted += gcnew UploadDataCompletedEventHandler( UploadDataCallback2 );
   client->UploadDataAsync( uri, method, data );
}
public static void UploadDataInBackground2(string address)
{
    WebClient client = new WebClient();
    Uri uri = new Uri(address);
    string text = "Time = 12:00am temperature = 50";
    byte[] data = System.Text.Encoding.UTF8.GetBytes(text);
    string method = "POST";

    client.UploadDataCompleted += new UploadDataCompletedEventHandler(UploadDataCallback2);
    client.UploadDataAsync(uri, method, data);
}
Public Shared Sub UploadDataInBackground2(ByVal address As String)

    Dim client As WebClient = New WebClient()
    Dim text As String = "Time = 12:00am temperature = 50"
    Dim data() As Byte = System.Text.Encoding.UTF8.GetBytes(text)
    Dim method As String = "POST"

    AddHandler client.UploadDataCompleted, AddressOf UploadDataCallback2
    Dim uri as Uri = New Uri(address)
                client.UploadDataAsync(uri, method, data)
End Sub

Następująca metoda jest wywoływana po zakończeniu przekazywania.

void UploadDataCallback2( Object^ /*sender*/, UploadDataCompletedEventArgs^ e )
{
   array<Byte>^data = dynamic_cast<array<Byte>^>(e->Result);
   String^ reply = System::Text::Encoding::UTF8->GetString( data );
   Console::WriteLine( reply );
}
private static void UploadDataCallback2(Object sender, UploadDataCompletedEventArgs e)
{
    byte[] data = (byte[])e.Result;
    string reply = System.Text.Encoding.UTF8.GetString(data);

    Console.WriteLine(reply);
}
Private Shared Sub UploadDataCallback2(ByVal sender As Object, ByVal e As UploadDataCompletedEventArgs)

    Dim data() As Byte = CType(e.Result, Byte())
    Dim reply As String = System.Text.Encoding.UTF8.GetString(data)

    Console.WriteLine(reply)
End Sub

Uwagi

Podczas tworzenia delegata należy zidentyfikować metodę UploadDataCompletedEventHandler , która będzie obsługiwać zdarzenie. Aby skojarzyć zdarzenie z programem obsługi zdarzeń, dodaj wystąpienie delegata do zdarzenia. Program obsługi zdarzeń jest wywoływany przy każdym wystąpieniu zdarzenia, o ile nie usunięto delegata. Aby uzyskać więcej informacji na temat delegatów programu obsługi zdarzeń, zobacz Obsługa i podnoszenie zdarzeń.

Metody rozszerzania

GetMethodInfo(Delegate)

Pobiera obiekt reprezentujący metodę reprezentowaną przez określonego delegata.

Dotyczy