2011-03-15 61 views
0

我正在调用具有单个参数的异步方法,它会根据参数返回结果。我使用不同的参数值多次调用该方法,但在Completed事件中,我得到的值相同。使用不同参数多次调用异步方法Silverlight 4.0

client.ListAllLookupValuesByTypeCompleted += client_ListAllAddressFormatCompleted; 
client.ListAllLookupValuesByTypeAsync("AddressFormat"); 

client.ListAllLookupValuesByTypeCompleted += client_ListAllPhoneFormatCompleted; 
client.ListAllLookupValuesByTypeAsync("PhoneFormat"); 



void client_ListAllAddressFormatCompleted(object sender, ListAllLookupValuesByTypeCompletedEventArgs e) 
     { 
      cmbAddressFormat.ItemsSource = e.Result; 
     } 


void client_ListAllPhoneFormatCompleted(object sender, ListAllLookupValuesByTypeCompletedEventArgs e) 
     { 
      cmbPhonePrintFormat.ItemsSource = e.Result; 
     } 

但是在e.Result中获得相同的值。

任何暗示。谢谢。

回答

0

您的方法可能会根据第一个参数返回不同的值,但无论您发送了什么内容,每次都会同时调用这两个处理程序。如果这是一个标准的Web服务引用,那么你应该看到一个对象userState参数可用,这可以用来确定要做什么。

client.ListAllLookupValuesByTypeCompleted += client_ListAllLookupValuesCompleted; 
client.ListAllLookupValuesByTypeAsync("AddressFormat", true); 
client.ListAllLookupValuesByTypeAsync("PhoneFormat", false); 



void client_ListAllLookupValuesCompleted(object sender, ListAllLookupValuesByTypeCompletedEventArgs e) 
     { 
      // e.UserState will either be false or true 
      if ((bool)e.UserState) 
       cmbAddressFormat.ItemsSource = e.Result; 
      else 
       cmbPhonePrintFormat.ItemsSource = e.Result; 
     }