views:

40

answers:

2

I'm writing a Windows Phone 7 app and want to programmtically access my Application bar buttons when the page loads. But these always appear to be null. I've tried accessing the button in the following events:

  • Page Contructor (after call to InitializeComponent)
  • Page's `Loaded' event handler
  • The Application Bars StateChanged event handler

There don't appear to be a Loaded event on the individual buttons either. Am I breaking the rules here? Why can't I access the application bar buttons with c#?

+1  A: 

Hi Will,

When I looked at this in one the CTPs, it was possible to access elements as I outlined in this thread.

Cannot change ApplicationBar items in code

Note that access to Application Bar elements is available through the type rather than the instance as you might initially expect.

Peter Torr clarified soon after that the Application Bar isn't quite a first class citizen in the framework. Explained here in more detail for your reference.

Why are the ApplicationBar objects not FrameworkElements? - Peter Torr's Blog

Mick N
Thanks for the link Mick. No matter what Peter Torr says I still can't believe it, seems like having c# access to everything the the UI tree is a given nowadays.
will
yea, sometimes you have to pick your battles I guess
Mick N
wise words... Anyways, I was able to access the menu item eventually by hooking into the Application Bars `StateChanged` handler. Then I could access the MenuItems and Buttons collections. But before you cheer it looks like at that point it's already been rendered and any text changes in the menu item don't appear in the UI.
will
Forgot to mention: the whole reason I wanted to know this is so I could localize the button text. How are other folks doing this???
will