Niels Brinch
Niels Brinch

Reputation: 3612

How to refer to buttons on ApplicationBar in Windows Phone development

In my Windows Phone application, I have some buttons on my ApplicationBar. In some cases buttons are hidden, in some cases buttons are disabled and in other cases all buttons are visible.

The way I currently use to refer to a button on my ApplicationBar (for example, if I have application logic that needs to disable it) is the following:

ApplicationBar.Buttons[1] as ApplicationBarIconButton;

I do not like this approach because it refers to the index of a button. If the list of buttons is relatively dynamic, this is asking for trouble.

Does anyone have a recommended way of referring to a button on ApplicationBar?

Upvotes: 2

Views: 226

Answers (1)

volpav
volpav

Reputation: 5128

What I usually do is I define buttons of interest as properties on a class, for example:

private ApplicationBarIconButton SaveButton { get; set; }

When the page is loaded, I keep the reference to the given button while constructing the application bar (programmatically):

SaveButton = new ApplicationBarIconButton(new Uri(...)) { Text = "Save" };
ApplicationBar.Buttons.Add(SaveButton);

This way the button is always available for manipulations via the property access.

Upvotes: 4

Related Questions