0

我想向事件传递三个额外的参数:

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);
    
    
            }
        }
    
    }
    
4

2 回答 2

0

它看起来像GeocodeCompletedEventArgs延伸AsyncCompletedEventArgsAsyncCompletedEventArgs具有UserState可用于存储异步事件状态信息的属性。此状态通常作为参数传递给引发事件的方法。

有关更多信息,请参阅此问题:Bing GeocodeService userState usage as custom additional parameter

于 2013-02-01T13:53:24.307 回答
0

您可以扩展 GeocodeService.GeocodeServiceClient 添加这些属性,然后在事件方法 geocodeService_GeocodeCompleted 中使用 sender 参数:

var service = (GeocodeService.GeocodeServiceClient) sender;

快速而肮脏(恕我直言)的版本是使用 lambda 表达式:

将参数传递给 EventHandler

于 2013-02-01T13:51:28.743 回答