Sujiz
Sujiz

Reputation: 1740

Got an exception when adding application bar programatically

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"
        }
    }

Upvotes: 0

Views: 294

Answers (1)

Olter
Olter

Reputation: 1139

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.

Upvotes: 1

Related Questions