0

In my application i want to add application bar at run time.I have a list containing the application bar names and the image uri .I have to add the application bar according to the list.But i got an exception "Specified argument was out of the range of valid values".Can any one give me a solution for this ? below is my code .

public void createObjectsForApplicationbar(List<AppBarDetails> appbarList)
    {
        int i = 0;
        foreach (Others menus in appbarList)
        {                                                          
          UpdateAppbarButton(i, menus.menu_image, menus.name, true, ApplicationBarIconButton_Click);
          i++;
        }
        ShowButtons(menuNames1);
    }                                                                                 



private void UpdateAppbarButton(int index, string uriString, string text, bool visibility, EventHandler handler)
    {
        ApplicationBarIconButton button1 = null;
        this.ApplicationBar = new ApplicationBar();
        this.ApplicationBar.IsVisible = true;
        this.ApplicationBar.Opacity = 1;
        this.ApplicationBar.IsMenuEnabled = true;
        if (this.ApplicationBar.Buttons.Count > index)
        {
            button1 = this.ApplicationBar.Buttons[index] as ApplicationBarIconButton;
        }

        if (button1 != null)
        {
            {
                this.ApplicationBar.Buttons.Remove(button1);
            }
        }
        if (visibility == true)
        {
            button1 = new ApplicationBarIconButton(new Uri(uriString, UriKind.RelativeOrAbsolute));
            button1.Text = text;
            button1.Click += handler;
            this.ApplicationBar.Buttons.Insert(index, button1);// here i got the exception "Specified argument was out of the range of valid values when the value of  i=1"
        }
    }
Sujiz
  • 1,720
  • 1
  • 20
  • 30
  • On which line was the exception thrown? – ColinE Dec 25 '12 at 09:21
  • @ColinE this.ApplicationBar.Buttons.Insert(index, button1); – Sujiz Dec 25 '12 at 09:36
  • i have got the exception when the value of i =1 – Sujiz Dec 25 '12 at 09:37
  • The point is that List.Insert cannot insert values to index, which is less than zero and more than List.Count. – Olter Dec 25 '12 at 10:43
  • i had removed the two if conditions i got the same exception ie if(this.ApplicationBar.Buttons.Count > index) { button1 = this.ApplicationBar.Buttons[index] as ApplicationBarIconButton; } if (button1 != null) { { this.ApplicationBar.Buttons.Remove(button1); } } – Sujiz Dec 25 '12 at 11:09
  • while debugging i got the count as 0 at if (this.ApplicationBar.Buttons.Count > index) – Sujiz Dec 25 '12 at 11:13
  • @Olter is there any other method to add application bar dynamically – Sujiz Dec 25 '12 at 11:14
  • Well... Let's think. You have a count=0, so the first and second conditions won't work. Then you have `if (visibility == true)` and try to _insert_ some value to the index = 1, but the count is 0. Sure, it will fail. You can just use **.Add** method to prevent it failing, but I'd better rethink the logic. It looks very strange. – Olter Dec 25 '12 at 11:20

1 Answers1

1

You should rethink the logic of your conditions.

if (this.ApplicationBar.Buttons.Count > index)
            {
                button1 = this.ApplicationBar.Buttons[index] as ApplicationBarIconButton;

                this.ApplicationBar.Buttons.Remove(button1);

                if (visibility == true)
                {
                    button1 = new ApplicationBarIconButton(new Uri(uriString, UriKind.RelativeOrAbsolute));
                    button1.Text = text;
                    button1.Click += handler;
                    this.ApplicationBar.Buttons.Insert(index, button1);
                }
            }

            else
            // insert it anyway?
            {
                if (visibility == true)
                {
                    button1 = new ApplicationBarIconButton(new Uri(uriString, UriKind.RelativeOrAbsolute));
                    button1.Text = text;
                    button1.Click += handler;
                    this.ApplicationBar.Buttons.Add(button1);
                }
            }

Not sure, what you really want, but that would work without exceptions.

Olter
  • 1,129
  • 1
  • 21
  • 40
  • thank you Olter but i got only one application bar as output even if there is 8 items in the list – Sujiz Dec 25 '12 at 12:06
  • Damn, I didn't even realize, that you are creating a new ApplicationBar each time, you call this event... Why did you give a name "Update" to it? Looks more like "Create & Update" in a single method. You'd better divide them into two separate methods and call "Create" in constructor and "Update" when you really want to update it. – Olter Dec 26 '12 at 06:43