2013-02-01 50 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