Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide Application Bar Icon Programmatically in a WP7 Silverlight Application?

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!

like image 548
user208662 Avatar asked Nov 20 '10 18:11

user208662


2 Answers

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);
like image 177
Den Delimarsky Avatar answered Nov 09 '22 14:11

Den Delimarsky


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.

like image 26
Dr Herbie Avatar answered Nov 09 '22 15:11

Dr Herbie