2013-02-01 51 views
0

我想通過三個額外的參數給事件:C#傳遞額外的參數給事件

geocodeService.GeocodeCompleted += new EventHandler<GeocodeService.GeocodeCompletedEventArgs>(geocodeService_GeocodeCompleted); 

的參數是

  • int id
  • string color
  • double heading

    private void Geocode(string strAddress, int waypointIndex, int id, string color, double heading) 
    { 
    
    
        // Create the service variable and set the callback method using the GeocodeCompleted property. 
        GeocodeService.GeocodeServiceClient geocodeService = new GeocodeService.GeocodeServiceClient("BasicHttpBinding_IGeocodeService"); 
    
        // NEED TO PASS id, color, heading TO THIS EVENT HANDLER 
        geocodeService.GeocodeCompleted += new EventHandler<GeocodeService.GeocodeCompletedEventArgs>(geocodeService_GeocodeCompleted); 
    
        GeocodeService.GeocodeRequest geocodeRequest = new GeocodeService.GeocodeRequest(); 
        geocodeRequest.Credentials = new Credentials(); 
        geocodeRequest.Credentials.ApplicationId = ((ApplicationIdCredentialsProvider)BingMap.CredentialsProvider).ApplicationId; 
        geocodeRequest.Query = strAddress; 
        geocodeService.GeocodeAsync(geocodeRequest, waypointIndex); 
    } 
    
    
    private void geocodeService_GeocodeCompleted(object sender, GeocodeService.GeocodeCompletedEventArgs e) 
    { 
        GeocodeResult result = null; 
    
        if (e.Result.Results.Count > 0) 
        { 
         result = e.Result.Results[0]; 
         if (result != null) 
         { 
          // this.ShowMarker(result); 
          this.ShowShip(result); 
    
    
         } 
        } 
    
    } 
    
+0

哪裏引發的事件?我懷疑在GeocodeAysnc方法中,有什麼方法可以看到這種方法嗎? – LukeHennerley

+0

@Solo你試過我的建議嗎? – giammin

回答

0

可以延長GeocodeService.GeocodeServiceClient添加這些屬性,然後在事件方法使用發送者參數geocodeService_GeocodeCompleted:

var service = (GeocodeService.GeocodeServiceClient) sender; 

的快速和骯髒(IMHO)版本是使用lambda表達式:

Pass parameter to EventHandler

0

它看起來像GeocodeCompletedEventArgs延伸AsyncCompletedEventArgsAsyncCompletedEventArgs的屬性爲UserState,可用於存儲異步事件的狀態信息。此狀態通常作爲參數傳遞給引發事件的方法。

更多信息請參見該問題:Bing GeocodeService userState usage as custom additional parameter

+0

我不同意你的回答,因爲UserState應該用於唯一標識異步任務並且不傳遞數據:http://msdn.microsoft.com/en-us/library/system.componentmodel.asynccompletedeventargs.userstate。 ASPX – giammin