0

I am developing a Windows phone app for which I want to get a list of first 15 object from my web service plus an extra item to represent the "Load next 15 items button" and bind it to a listbox. On selection of the last element or index(buttton), I want to remove the last item(button) from the list and call the service again to get the next 15 items plus the button item which will again add up to the current list without clearing the list. And the same process goes on as we select the last item from the listbox. My problem is I am able to get it correctly for the first round. In the second round, the next 15 element is added to the list twice. I don't know what is the real problem. I am not getting any error or exception also.

Below is what I have done so far :

private void ListBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        int LastIndex=app.imgItem.Count - 1;
        if (ListBox1.SelectedIndex == LastIndex)
        {
            app.imgItem.Remove(app.imgItem[app.imgItem.Count-1]);
            proxy.SelectNextItemsCompleted += new EventHandler<ServiceReference1.SelectNextItemsCompletedEventArgs>(proxy_SelectNextItemsCompleted);
            int id = app.imgItem.Last().Id;
            proxy.SelectNextItemsAsync(categoryID, id);
        }

    }

void proxy_SelectNextItemsCompleted(object sender, ServiceReference1.SelectNextItemsCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            MessageBox.Show("Cannot load data.");
            return;
        }
        else
        {
                foreach (var item in e.Result)
                {
                    ImgData idata = new ImgData();
                    idata.CategoryID = item.CategoryID;
                    idata.ImageID = item.ImageID;
                    idata.ImageTitle = item.ImageTitle;
                    idata.Thumbnail = item.Thumbnail;
                    app.imgItem.Add(idata);
                }
        }
        ImageData btnData = new ImageData();
        btnData.CategoryID = 0;
        btnData.ImageID = 0;
        btnData.ImageTitle = "";
        btnData.Thumbnail = "Images/loadButton.jpg";
        app.imgItem.Add(btnData);
        ListBox1.ItemsSource = app.imgItem;
    }
Shan
  • 435
  • 6
  • 19

1 Answers1

0

It looks like you're re-registering an event handler in the SelectionChanged event handler, and as a result, the SelectNextItemsCompleted handler is probably getting called twice the second time around:

proxy.SelectNextItemsCompleted += new EventHandler<ServiceReference1.SelectNextItemsCompletedEventArgs>(proxy_SelectNextItemsCompleted);

You should probably be registering that event handler somewhere else where the code only runs once.

JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • I think what you are telling is right but I don't know how to handle it. I want to call "proxy.SelectNextItemsCompleted" eventhandler on selection of the last item of the list. If you have any idea to handle it, please let me know . Thank you. – Shan Jan 15 '13 at 12:18
  • From looking at your code, it looks like `SelectNextItemsCompleted` is a callback handler for `proxy.SelectNextItemsAsync()`. This means you don't need to (should not) invoke it manually. It will be automatically invoked when `proxy.SelectNextItemsAsync()` finishes executing (as long as you have registered the event handler at some point during the program's execution). – JLRishe Jan 15 '13 at 12:26
  • Thanks a lot, I have used a counter to check whether "SelectNextItemsCompleted" is invoked before or not. Now it working perfectly fine. – Shan Jan 16 '13 at 07:14