共用方式為


UploadDataCompletedEventHandler 代理人

定義

代表將處理 UploadDataCompletedWebClient 事件的方法。

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)

參數

sender
Object

事件的來源。

範例

下列程式代碼範例示範如何以異步方式上傳數據。

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

上傳完成時會呼叫下列方法。

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

備註

建立 UploadDataCompletedEventHandler 委派時,必須識別處理事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。 如需事件處理程式委派的詳細資訊,請參閱 處理和引發事件

擴充方法

GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。

適用於