DownloadStringCompletedEventHandler 代理人
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表將處理 DownloadStringCompleted 之 WebClient 事件的方法。
public delegate void DownloadStringCompletedEventHandler(System::Object ^ sender, DownloadStringCompletedEventArgs ^ e);
public delegate void DownloadStringCompletedEventHandler(object sender, DownloadStringCompletedEventArgs e);
type DownloadStringCompletedEventHandler = delegate of obj * DownloadStringCompletedEventArgs -> unit
Public Delegate Sub DownloadStringCompletedEventHandler(sender As Object, e As DownloadStringCompletedEventArgs)
參數
- sender
- Object
事件的來源。
範例
下列程式代碼範例示範異步下載字串。
// Sample call : DownloadStringInBackground2 ("http://www.contoso.com/GameScores.html");
void DownloadStringInBackground2( String^ address )
{
WebClient^ client = gcnew WebClient;
Uri ^uri = gcnew Uri(address);
// Specify that the DownloadStringCallback2 method gets called
// when the download completes.
client->DownloadStringCompleted += gcnew DownloadStringCompletedEventHandler( DownloadStringCallback2 );
client->DownloadStringAsync( uri );
}
// Sample call : DownloadStringInBackground2 ("http://www.contoso.com/GameScores.html");
public static void DownloadStringInBackground2(string address)
{
WebClient client = new WebClient();
Uri uri = new Uri(address);
// Specify that the DownloadStringCallback2 method gets called
// when the download completes.
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCallback2);
client.DownloadStringAsync(uri);
}
' Sample call : DownloadStringInBackground2 ("http:' www.contoso.com/GameScores.html")
Public Shared Sub DownloadStringInBackground2(ByVal address As String)
Dim client As WebClient = New WebClient()
' Specify that the DownloadStringCallback2 method gets called
' when the download completes.
AddHandler client.DownloadStringCompleted, AddressOf DownloadStringCallback2
Dim uri as Uri = New Uri(address)
client.DownloadStringAsync(uri)
End Sub
下載完成時會呼叫下列方法。
void DownloadStringCallback2( Object^ /*sender*/, DownloadStringCompletedEventArgs^ e )
{
// If the request was not canceled and did not throw
// an exception, display the resource.
if ( !e->Cancelled && e->Error == nullptr )
{
String^ textString = dynamic_cast<String^>(e->Result);
Console::WriteLine( textString );
}
}
private static void DownloadStringCallback2(Object sender, DownloadStringCompletedEventArgs e)
{
// If the request was not canceled and did not throw
// an exception, display the resource.
if (!e.Cancelled && e.Error == null)
{
string textString = (string)e.Result;
Console.WriteLine(textString);
}
}
Private Shared Sub DownloadStringCallback2(ByVal sender As Object, ByVal e As DownloadStringCompletedEventArgs)
' If the request was not canceled and did not throw
' an exception, display the resource.
If e.Cancelled = False AndAlso e.Error Is Nothing Then
Dim textString As String = CStr(e.Result)
Console.WriteLine(textString)
End If
End Sub
備註
建立 DownloadStringCompletedEventHandler 委派時,必須識別處理事件的方法。 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。 如需事件處理程式委派的詳細資訊,請參閱 處理和引發事件。
擴充方法
GetMethodInfo(Delegate) |
取得表示特定委派所代表之方法的物件。 |