Animated Silverlight Calls and Anonymous Methods

I know there are a couple of debates about this. Anyway, I have a few cases where I need to populate ComboBox items based on enums returned from WCF service.

To keep the code clean, I started this approach. After thinking about it more, I don't think it works as originally thought ...

I am throwing this out for guidance / tips / code snippets on how you would do this or how it is currently being done.

I may be forced to have a separate, non-anonymous method, procedure. I hate doing it for something like this, but at the moment I don't see it working any differently ...

EventHandler<GetEnumerationsForTypeCompletedEventArgs> ev = delegate(object eventSender, GetEnumerationsForTypeCompletedEventArgs eventArgs)
{
    if (eventArgs.Error == null)
    {
        //comboBox.ItemsSource = eventArgs.Result;
        //populate combox for display purposes (for now)
        foreach (Enumeration e in eventArgs.Result)
        {
            ComboBoxItem cbi = new ComboBoxItem();
            cbi.Content = e.EnumerationValueDisplayed;
            comboBox.Items.Add(cbi);
        }
        // remove event so we don't keep adding new events each time we need an enumeration
        proxy.GetEnumerationsForTypeCompleted -= ev;
    }
};
proxy.GetEnumerationsForTypeCompleted += ev;
proxy.GetEnumerationsForTypeAsync(sEnumerationType);

      

Basically in this example, we are using ev to hold the anonymous method, so we can use ev from the method to remove it from the events raised. This prevents this method from being called more than once.

I suspect that the ComboBox local variable declared before this call, but inside the same method, is not always meant for the ComboBox, but cannot check it yet. I can add a tag to it to do some tests and fill it for validation.

Sorry if this is not clear. I can develop more if needed.

Thanks.

0


a source to share


1 answer


The way I did it is like this:

var query = from t in GetFreshContext().Employee select t;
var dsQuery = (DataServiceQuery<Employee>)query;
dsQuery.BeginExecute(result =>
{
    ComboEmployees.ItemsSource = dsQuery.EndExecute(result).ToArray();
}, null);
ComboEmployees.DisplayMemberPath = "FullName";

      



I wrote a post regarding some other issues and this .

+1


a source







All Articles