vinco83
vinco83

Reputation: 487

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!

Upvotes: 1

Views: 300

Answers (2)

mantov5
mantov5

Reputation: 45

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

Upvotes: 0

a little sheep
a little sheep

Reputation: 1466

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

Upvotes: 1

Related Questions