13

I have a Windows Phone 7 application built in Silverlight. This application makes use of the application bar. If the has purchased the application, I want to hide one of the buttons in the application bar. However, I've noticed that the ApplicationIconButton class does not expose a "Visibility" property. At the same time, I did not see a way to dynamically populate the application bar at runtime.

Can anybody provide some insight into this? Is this possible? If so, how?

Thanks!

2 Answers 2

32

Application bar buttons work in an index-based way rather than object-based like you would expect. Therefore, you need to specify a button index whenever you want to perform a specific action on it (e.g. disable).

For example:

ApplicationBarIconButton b = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
b.IsEnabled = false;

This being said, you can create new ApplicationBarIconButton instances and pass them to ApplicationBar:

for (int i = 0; i < 2; i++)
{
    ApplicationBarIconButton b = new ApplicationBarIconButton();
    b.Text = i.ToString();
    b.IconUri = new Uri("/Images/icon1.png", UriKind.Relative);
    ApplicationBar.Buttons.Add(b);
}

When removing buttons, you can simply use RemoveAt, given that you know the index of the button to remove:

ApplicationBar.Buttons.RemoveAt(0);
13

I use the following method to alter the application bar buttons in my code:

private void UpdateAppbarButton(int index, string uriString, string text, bool visibility, EventHandler handler)
{
    ApplicationBarIconButton button1 = null;

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

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

The uriString is the relative path to the icon that I wish to display on the app button. You can probably adapt this code for your own scenarios.

Essentially, instead of setting a button visible or not, you have to remove the button (if it's there) or re-add it if it's not.

3
  • I need to give the IconUri for ApplicationBarIconButton as http url. Is this possible? I am getting the below exception. "Path cannot be absolute".
    – Sujiz
    Dec 23, 2012 at 18:08
  • Hi Sujiz, Did you set the UriKind parameter to be absolute? Although I don't actually know if HTTP addresses will work or not, it's worth a quick try.
    – Dr Herbie
    Dec 27, 2012 at 14:13
  • i have set the UriKind to absolute but it didn't work for me.is there any other solution to load the application bar icon from http url?
    – Sujiz
    Dec 28, 2012 at 4:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.