The Windows Phone Application Bar has been a true headache for developers since day one, given that this object is not a FrameworkElement.
What this means is that we are unable to apply Styles or Templates to an ApplicationBar object, but most important, we are unable to bind to its properties, something that is a big issue when you what to use MVVM pattern in you apps.
Surely MVVM is not a closed pattern, and as such there are alternatives to overcome this issue (like this one or this one)!
In my case, in order to access the ApplicationBar in my apps in a more ”MVVM‘ed” way, I created the ApplicationBarBehavior that you can find in the Cimbalino Windows Phone Toolkit!
This Behavior, when applied to a PhoneApplicationPage or it’s LayoutRoot element, allows to create and maintain a fully bindable Application Bar, and as such applying a more consistent MVVM pattern.
Here’s a small sample code for it:
<!-- remaining code --><Grid x:Name="LayoutRoot" Background="Transparent"> <i:Interaction.Behaviors> <cimbalino:ApplicationBarBehavior> <cimbalino:ApplicationBarIconButton Command="{Binding AddItemCommand, Mode=OneTime}" IconUri="/Images/appbar.add.rest.png" Text="add" IsVisible="{Binding IsSelectionDisabled}" /> <cimbalino:ApplicationBarIconButton Command="{Binding EnableSelectionCommand, Mode=OneTime}" IconUri="/Images/appbar.manage.rest.png" Text="select" IsVisible="{Binding IsSelectionDisabled}" /> <cimbalino:ApplicationBarIconButton Command="{Binding DeleteItemsCommand, Mode=OneTime}" CommandParameter="{Binding SelectedItems, ElementName=ItemsMultiselectList}" IconUri="/Images/appbar.delete.rest.png" Text="delete" IsVisible="{Binding IsSelectionEnabled}" /> </cimbalino:ApplicationBarBehavior> </i:Interaction.Behaviors> <!-- remaining code --></Grid><!-- remaining code -->
In this small code sample we can see the ApplicationBarBehavior and in it, some ApplicationBarIconButton (like you would do in the current one), and quickly we notice some new properties like Command, CommandParamenter, and IsVisible (something we can’t even find in the base Windows Phone ApplicationBar); obviously, even the properties Text
and IconUri
are bindable, allowing for easy localization! ;)