多次事件处理程序触发,为什么? [英] Multiple event handler firings, why?

查看:48
本文介绍了多次事件处理程序触发,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难解决这个问题.我已经呆了3个小时,但我仍然找不到原因.这是代码:

I have a tough time trying to solve this problem. I have been at for 3 hours, and still I couldn't find out why it is doing this. Here is the code:

private void Catagory_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int selectedCategoryId = categoryIdList[categoryListBox.SelectedIndex];

        client.GetItemsAsync(selectedCategoryId);
        client.GetItemsCompleted += 
            new EventHandler<GetItemsCompletedEventArgs>(client_GetItemsCompleted);
    }

void client_GetItemsCompleted(object sender, GetItemsCompletedEventArgs e)
{
        itemIdList.Clear();
        itemNameList.Clear();
        itemNumberList.Clear();
        itemDisplayList.Clear(); //Clears the Display List Items

        if (e.Error == null)
        {
            itemIdList = e.ItemIDList;
            itemNumberList = e.itemNumber;
            itemNameList = e.Result;

            for (int i = 0; i < itemIdList.Count; i++)
            {
                itemDisplayList.Add(new ItemDisplay { itemNumber = itemNumberList[i], itemName = itemNameList[i] });
            }

            //Populating the listbox controll with the itemDisplaylist...
            Items.ItemsSource = itemDisplayList;
        }
        else
        {
            MessageBox.Show("Problem in getting the items list.");
        }
    }

当我第一次更改类别时,它可以很好地工作.完美地说,我的意思是它调用函数 GetItemsAsync(selectedCategoryId)并获取结果并调用事件处理程序 client_GetItemsCompleted(),事件处理程序的内部工作方式如下:它应该为列表设置适当的数据,并在列表框中显示itemNumber和itemName.但是,当我再次更改类别以获取不同的项目时,它无法正常工作,它的作用是清除列表并按预期填充列表,运行for循环并填充名为 Items ,但由于某种原因,它再次移至函数顶部并清空所有列表.请告诉我为什么它再次执行该功能?当我再次选择另一个类别时,它将执行事件处理程序3次,然后执行4次,依此类推.知道为什么这样做吗?

When I change the category the first time it works perfectly. By perfectly, I mean that it calls the function GetItemsAsync(selectedCategoryId) and grabs the results and calls the event handler client_GetItemsCompleted() and the inner working of the event handler works as it is supposed to, it sets the lists with the proper data and displays the itemNumber and the itemName in the list box . But, when I change the category again to get different items, it doesn't work properly, what it's doing is that it clears the lists and populates the lists as it is supposed to, runs the for loop and populates the listBox called Items but for some reason it goes to the top of the function again and empties all the lists. Please tell my why it is executing the function again? And when I choose another category again, it executes the event handler 3 times and then 4 times and so on. Wnow why it is doing this?

推荐答案

每次执行时:

  client.GetItemsCompleted += 

您向事件添加了订阅者,因此第二次将触发两次(第三次触发三次,等等.)

You add a subscriber to the event, so the second time it will fire twice (the third time triple times, etc..).

已完成方法中的任一取消订阅(-=)

Either unsubscrice ( -= ) in the completed method:

void client_GetItemsCompleted(object sender, GetItemsCompletedEventArgs e)
{
    try {
       /* .... */
    }
    finally {
        client.GetItemsCompleted -= 
            new EventHandler<GetItemsCompletedEventArgs>(client_GetItemsCompleted);
    }
}

或在每次调用前初始化客户端对象.

or initiate the client object before every call.

var client = new ...();
client.GetItemsAsync(selectedCategoryId);
client.GetItemsCompleted += 
            new EventHandler<GetItemsCompletedEventArgs>(client_GetItemsCompleted);

这篇关于多次事件处理程序触发,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆