0

I have to add application bars at run time i have tried some codes and that does not work for me can any one suggest me a solution ? Here is my code

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

    }

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;

            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

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


    }

When i run this code i got only one button as output even if there is 8 items in the list .I got this code from stackoverflow

Community
  • 1
  • 1
Sujiz
  • 1,720
  • 1
  • 20
  • 30

1 Answers1

1

You should not create new ApplicationBar every time you add a button

remove this line:

this.ApplicationBar = new ApplicationBar();

also

this.ApplicationBar.Buttons.Add(Buttons[text]);

is wrong. You create new button in this code block. So you shoud add this button.

if (visibility == true)
{
    button1 = new ApplicationBarIconButton(new Uri(uriString, UriKind.RelativeOrAbsolute));
    button1.Text = text;
    button1.Click += handler;
    this.ApplicationBar.Buttons.Add(button1);
}
Dmitrii Dovgopolyi
  • 6,231
  • 2
  • 27
  • 44
  • ok but i got a null reference @ line this.ApplicationBar.Buttons.Add(Buttons[text]); but this.ApplicationBar.Buttons is not null – Sujiz Dec 26 '12 at 13:56
  • @Sujiz, what did you try to do with this `this.ApplicationBar.Buttons.Add(Buttons[text]);`? I've given the correct version, why you've changed it? :) – Olter Dec 27 '12 at 07:02
  • @Dmitry Dovgopoly i have tried your code but i didnt got any application bar.is there need to call any additional function to show the application bar? – Sujiz Dec 28 '12 at 04:33