Skip to main content

Windows Phone 8 - Application bar command binding MVVM

This is a short post in which i will explain how on Windows phone 8 to bind the application bar button or menu item, first this is only a fix for the BindableApplicationbar which supports windows phone 7 only, i just made it support windows 8 no features added or anything.
i have uploaded the dll file here so it can be accessible easily here is the link to download
http://sdrv.ms/RApUal
now that you got the link lets check how we gonna use it
you can refer to BinableApplicationbar 
or check out the code here that i actually used in my app and i already read it there ! 
  1. Add a reference to the BindableApplicationBar library here is the link again http://sdrv.ms/RApUal
  2. Add XML namespace declaration in your page's XAML:
    xmlns:bar="clr-namespace:BindableApplicationBar;assembly=BindableApplicationBar"
  3. Set Bindable.ApplicationBar property on your page code as in the snippet below:
    <phone:PhoneApplicationPage>
        …
        <bar:Bindable.ApplicationBar>
            <bar:BindableApplicationBar>
            </bar:BindableApplicationBar>
        </bar:Bindable.ApplicationBar>

    </phone:PhoneApplicationPage>
  4. Bind properties of the BindableApplicationBar to your view model. Available properties:
    • BackgroundColor
    • BindableOpacity
    • Buttons (also the default Content property – you can just put BindableApplicationBarButton XML elements inside of the BindableApplicationBar XML element).
      BindableApplicationBarButton properties:
      • Command
      • CommandParameter
      • IconUri
      • IsEnabled
      • Text (note that the control can only fit up to about 11 characters and will trim anything over that limit)
    • ButtonsSource (alternative to Buttons – allows to bind to a collection of view models in a similar fashion to ListBox.ItemsSource)
    • ButtonTemplate (used with ButtonsSource in a similar way ListBox.ItemTemplate is used with ListBox.ItemsSource)
    • ForegroundColor
    • IsMenuEnabled
    • IsMenuVisible
    • IsVisible
    • MenuItems (similar to Buttons, but for BindableApplicationBarMenuItem elements and needs to be specified explicitly as <BindableApplicationBar.MenuItems/>).
      BindableApplicationBarMenuItem properties:
      • Command
      • CommandParameter
      • IsEnabled
      • Text (note the text can fit about 28 digit glyph-wide characters and will trim anything over that limit)
    • MenuItemsSource
    • MenuItemTemplate
    • Mode
  5. Note ApplicationBar limitations – the total number of buttons defined through either the regular ApplicationBar, BindableApplicationBar.Buttons or BindableApplicationBar.ButtonsSource can’t be higher than 4. MenuItems max out at 50. If you go over – you will get an InvalidOperationException with a message like “Too many items in list”.
Sample code
<bar:Bindable.ApplicationBar>
    <bar:BindableApplicationBar
        IsVisible="{Binding BarIsVisible}"
        IsMenuVisible="{Binding IsMenuVisible, Mode=TwoWay}"
        IsMenuEnabled="{Binding IsMenuEnabled}"
        ForegroundColor="{Binding ForegroundColor, Converter={StaticResource DoubleToColorConverter}}"
        BackgroundColor="{Binding BackgroundColor, Converter={StaticResource DoubleToColorConverter}}"
        BindableOpacity="{Binding Opacity}"
        Mode="{Binding Mode}"
        MenuItemsSource="{Binding MenuItems}"
        ButtonsSource="{Binding Buttons}">
        <!--<bar:BindableApplicationBar.MenuItemTemplate>
            <DataTemplate>
                <bar:BindableApplicationBarMenuItem
                    Text="{Binding Text}"
                    Command="{Binding Command}"
                    CommandParameter="{Binding CommandParameter}"/>
            </DataTemplate>
        </bar:BindableApplicationBar.MenuItemTemplate>-->
        <bar:BindableApplicationBarButton
            Text="{Binding IconButtonText}"
            IconUri="{Binding IconUri, FallbackValue=/Icons/Dark/appbar.add.rest.png}"
            IsEnabled="{Binding ButtonIsEnabled}" />
        <bar:BindableApplicationBarButton
            Text="XAML Btn 2"
            IconUri="/Icons/Dark/appbar.next.rest.png"
            Command="{Binding TestCommand}"
            CommandParameter="{Binding TestCommandParameter}" />
        <bar:BindableApplicationBar.MenuItems>
            <bar:BindableApplicationBarMenuItem
                Text="{Binding MenuItemText}"
                IsEnabled="{Binding MenuItemIsEnabled}" />
            <bar:BindableApplicationBarMenuItem
                Text="XAML MnuIt 2"
                Command="{Binding TestCommand2}"
                CommandParameter="{Binding TestCommand2Parameter}" />
        </bar:BindableApplicationBar.MenuItems>
    </bar:BindableApplicationBar>
</bar:Bindable.ApplicationBar>


I hope this was any near of a help to you, thanks for reading



Comments

  1. I cant add dll as a reference in VS. It says A reference to a higher version or incompatible assembly cannot be added to the project. My phone project has OS 8.0 selected.

    ReplyDelete
    Replies
    1. you need not to add the dll, get the source code of the project and add it to your solution, as explained above and then you can do the fix that i have shared with you.

      Delete
  2. same issue. it looks that this application bar is not suited for wp8. Is there any alternative ?

    ReplyDelete
  3. I tapped the iconbutton I bound a command to and nothing happens

    ReplyDelete
  4. I mean, when I click or tap the buttons nothings happens, and wasn't so when I use normal phone:applicationbariconbutton

    ReplyDelete

Post a Comment

Popular posts from this blog

Xamarin Forms Core components Part 1 Dependency Service

Intro Xamarin Forms is a collection of controls that can be used and rendered on multiple platforms, and in order of them to function as they are suppose to, they do need a set of core components that defines the way these controls, in how they are created, how they are rendered to how they are used, of course every platform is different and sometimes a platform specific extra configuration is required, specially that there are so many differences between the different platforms in matter of design, user experience and operating system behavior. So one of the core components of  Xamarin  Forms is the Dependency Service, and by the name suggest it does act as the dependency resolver for all forms controls, if you are not familiar with IOC " I nversion O f C ontrol" and Dependency Injection please refer to the link for a quick intro i wrote a while ago on IOC  IOC Part 1 - Inversion of control Principle .  And as of such the Dependency Service is the concrete inboxe

Xamarin Forms: XAML Creating Custom Controls The MVVM Way

Intro For a growing UI page there always comes a need to create sub views that can be used inside a bigger view, and for that we need custom controls, which are controls that are derived either from a layout or a simple view which is the basic control for almost any UI component in Xamarin forms, and therefore for a start i will use that as an example. And in such example we will create a custom control with a set of bindable properties and explain how they work, also how to add an event that can also be bound to Commands Lets Create the control  I have just went ahead and created a custom control that inherits from Xamarin.Forms.View and have some properties such color and checked all properties to be bound to by the ViewModel serving as the datacontext of the this control. here is the code below using System; using Xamarin.Forms; namespace UserControls { public class CustomView : View { public Color Color { get ; set ;