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

Windows 8 – XAML/C# how to Add multi push pins to map control Bing maps

Greetings readers I promised you in my previous post that I am gonna go a little bit deeper in the Bing map control and I will should more advanced scenarios and one is push pins, Basically I am going to tell you how you can add push pin to your map and also how to dynamically populate those push pins using binding Refer to my previous article Using bing maps part one  for more details cause I am going build on it so if you ever feel that you don’t understand something just click the link and spend a few minutes reading it, I promise you it will prove worthy. First lets add a normal push pin to our map  < Grid Background = " {StaticResource ApplicationPageBackgroundThemeBrush} " > < bm : Map x:Name = " MyMap " Width = " 640 " Height = " 480 " Credentials = " {StaticResource BingMapsApiKey} " > < bm : Map.Children > < bm : Pushpin x:Name = ...

Windows 8 RTM Using bing map control. How to use the map control in a metro (modern) style App

This is a briefing article on how to start using Bing map control using XAML & C#, to begin with you must first follow a set of instruction to get a license key for using Bing maps. Lets talk first about bing maps and the map control Bing map control is a part of the Bing SDK for windows 8 modern style apps About the SDK as Microsoft says : Bing Maps SDK for Metro style apps (RTM Beta)   combines the power of Windows 8 and   Bing Maps ™ to provide an enhanced mapping experience for modern style apps. Developers can use this Bing Maps control to incorporate the latest road maps and aerial views into a Windows 8 Metro style app. This SDK includes controls for apps built using JavaScript, as well as apps built using C#, C++, and Visual Basic, and requires a   Bing Maps Key   for a Windows Metro style app. (If you have an existing “Metro style apps (BETA)” key, it will still work until the evaluation period expires.) Now lets get started and here...