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

(AsyncWebClient) Async Webclient for windows phone 8

Using windows phone web client is pretty common but the thing is you can not use it using Async -> Await mechanism so i used threading to create an async functionality for The Download string and upload string methods here is the code below // Comment public class AsyncWebClient { public Task DownloadString(Uri uri) { var task = new TaskCompletionSource(); try { var client = new WebClient(); client.DownloadStringCompleted += (s, e) => { task.SetResult(e.Result); }; client.DownloadStringAsync(uri); } catch (Exception ex) { task.SetException(ex); } return task.Task; } public Task UploadString(Uri uri, string content) { var task = new TaskCompletionSource(); try { var client…

RuntimeBinderException cannot resolver property in Unit Test in case of dynamic return.. Solved

Xamarin UI Tests – Deep dive Part 1

IntroAs the title may describe this is the first of the series of articles that will cover the UI Test of Xamarin in a deep dive, we will start simple and then dig deeper as we go. Since this is very well the first of the series, it will mostly cover up the architecture and the testing technique that Xamarin UI Tests uses.

The Technology
Xamarin UI Test is an automation testing framework similar to selenium, Watir, Watin (.net), Robot and Sikuli, if you have used BDD (Behavioral Driven Development) or a more advanced TDD (Test Driven Development) approach at least you must have come across one of these frameworks, and in that perspective comes Xamarin UI Testing as an automation framework designed specifically for Xamarin Automation testing, For starter Xamarin UI Tests was not build from scratch, instead it was built on top of another UI Automation testing framework that is targeting android and iOS sepecifically which is Calabash, I have to say that this choice was made wisely as of n…