简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM