Reputation: 1740
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
Upvotes: 0
Views: 557
Reputation: 6301
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);
}
Upvotes: 1