2

I have Silverlight app, which has some combo boxes, which I want to fill with fields names from SharePoint list. Every ComboBox can have different fields from this list, e.g. ComboBoxA holds user field and ComboBoxB number fields. Now, I don't want to create different ClientRequestSucceededEventHandler and ClientRequestFailedEventHandler for every ComboBox. I don't want to "simulate" asynchronous processing either.

My idea was to pass some param to these event handlers (e.g. reference to destination combo box & items collection):

    void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
        Context = ClientContext.Current;

        //load query for getting user fields

        Context.ExecuteQueryAsync(fieldsCallback_Succeeded(cbUserFields, userFields), fieldCallback_Failed);

        //load query for getting number fields

        Context.ExecuteQueryAsync(fieldsCallback_Succeeded(cbUserFields, numberFields), fieldCallback_Failed);
    }

    void fieldsCallback_Succeeded(object sender, ClientRequestSucceededEventArgs e)
    {
        FieldsQueryParams queryParams = sender as FieldsQueryParams;
        this.Dispatcher.BeginInvoke(() => queryParams.cbToFill = queryParams.Fields);
    }

OR

    void fieldsCallback_Succeeded(object sender, ClientRequestSucceededEventArgs e, ComboBox cbToFill, IEnumerable<Field> fields)
    {
        this.Dispatcher.BeginInvoke(() => cbToFill.ItemsSource = fields);
    }

So the question is: how to pass some param to these event handlers (e.g. reference to destination control). Or how to resolve this problem in other way?

Marcin Robaszyński
  • 772
  • 1
  • 11
  • 21

1 Answers1

1

Inherit the class, create a specialized instance that takes arguments, either a list, object or specifically typed objects as you like. You should be able to pass it in and then process the result as you're envisioning since it has all the implementation it expects to see and evaluates to the correct type. Since it's a callback, I don't think you'll need to cast it.

It expects to see:

public virtual void ExecuteQueryAsync(
    ClientRequestSucceededEventHandler succeededCallback,
    ClientRequestFailedEventHandler failedCallback
)
animuson
  • 53,861
  • 28
  • 137
  • 147
Kay
  • 11
  • 1