开发者

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!


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);


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜