Wednesday, August 29, 2012

Generic Async Completed method

Generic Async Completed method

I have a silverlight page with a listbox and a combobox...

Based on what the user clicks in the list box, I want to populate the dropdownbox. The completed event is the same for each item in the list box (items include "BaseTypes", "Bays", "Face", etc)

How can I make the completed method generic so I don't have to have one for each call?

private void lstEdits_SelectionChanged(object sender, SelectionChangedEventArgs e) {     ServiceCylinderClient obj = new ServiceCylinderClient();     obj.GetBaysCompleted += new EventHandler<GetBaysCompletedEventArgs>(GetBaysCompleted(this, baysEventArgs));      string selectedItem = lstEdits.SelectedItem as string;      switch selectedItem     {          case "BaseTypes":            obj.GetBaseTypesCompleted += new EventHandler<GetBaseTypesCompletedEventArgs>(GetBaysCompleted(this, baysEventArgs));            obj.getGetBaseTypesAsync();            break;         case "Bays":            obj.GetBaysCompleted += new EventHandler<GetBaysCompletedEventArgs>(GetBaysCompleted(this, baysEventArgs));            obj.getGetBaysAsync();            break;     } } 

As it stands now, I will have to have a "completed method" for each call, but since they would all do the same thing (just set the list box items source)..i'd like to make it generic to simplify things.

void GetBaseTypesCompleted(object sender, getBaseTypesCompletedEventArgs e) {     lstEdits.ItemsSource = e.Result; }  void GetBaysCompleted(object sender, getBaysCompletedEventArgs e) {     lstEdits.ItemsSource = e.Result; } 

Thanks in advance!

Answers & Comments...

Answer: 1

I believe you would need to use reflection to read the 'Result' property off the 'CompletedEventArgs' as they do not all come from a base type that exposes 'Result'.

You should be able to do something like the following:

lstEdits.ItemsSource = (IEnumerable)e.GetType().GetProperty("Result").GetValue(e, null); 
by : a little sheephttp://stackoverflow.com/users/1470636

Answer: 2

I think it dont have easy solution for this problem,because every completedmethod has different EventArgs for different result.

by : mantovhttp://stackoverflow.com/users/1611122




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog