Windows Phone 7 - Creating that elusive reusable Application Bar
22 July 2010
Windows Phone 7 application pages can have an application bar placed on them.
However you can’t bind to them and you have to use event handlers on them (rather than commanding via MVVM Light) which means messing up your code behind.
I wrote a previous post on how to get a dynamic application bar that talked to the view model using MVVM Light messaging for example.
In fact, you can bind an application bar directly to the page via your View Model!
In our case, we have a DefaultAppBar defined as a dependency property on our ViewModelBase for binding the Application Bar to the View. We then create the Application Bar programmatically when the View Model is constructed, see the example below:
Disclaimer: It’s possible you may experience a bug (at least in the beta) with the rendering of the page as it redraws to include the application bar binding . This is unfortunate but can be remedied if you manually assign the Application bar in the constructor of the page’s code behind.
If you need view model specific application bars you can simply add these to other view models, but i would imagine most applications with multiple xaml pages will benefit from the above approach which eliminates the need to copy and paste xaml and cs around your application.