Corrado's Blog 2.0

Online thoughts of a technology funatic

Xamarin.Forms.Behaviors v.1.1.0

I’ve pushed an update to Xamarin.Forms.Behaviors library, fixed some bugs and added Relative Commanding to EventToCommand Behavior.

What is Relative Commanding?

It is a way to let EventToCommand invoke a Command that is exposed by a ViewModel that’s not part of actual BindingContext, typical example is when you are inside an ItemTemplate (ViewCell in Xamarin Forms) and you need to invoke a command that’s hosted inside another viewmodel.

To support this feature I’ve added two new properties to EventToCommand: CommandName and CommandContext, former is the name (string) of the Command you want to invoke, latter is the BindingContext that exposes that command.

How can you link CommandContext with desired BindingContext? Use the new RelativeContext markup extension exposed by Behaviors library to ‘link’ a BindingContext to another (CommandContext in case of EventToCommand)

Here’s a sample taken from GitHub repo.

MainViewModel exposes both an ObservableCollection of Items and a NickSelectedCommand and is also bound to MainPage’s BindingContext, here’s how you can let ListView’s items invoke MainViewModel command.

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
      xmlns:b="clr-namespace:Xamarin.Behaviors;assembly=Xamarin.Behaviors"
        x:Class="Xamarin.Behaviors.Demo.Views.MainView"
      x:Name="MainPage"> 

  <ListView ItemsSource="{Binding Items}"
      IsGroupingEnabled="false"
      RowHeight="60">
   <ListView.ItemTemplate>
    <DataTemplate>
     <ViewCell>
      <StackLayout Orientation="Horizontal" Padding="10" >
       <StackLayout Orientation="Vertical"  HorizontalOptions="FillAndExpand" Spacing="-50"  >
        <Button Text="{Binding NickName}">
         <b:Interaction.Behaviors>
          <b:BehaviorCollection>
           <b:EventToCommand CommandNameContext="{b:RelativeContext MainPage}"
                 EventName="Clicked"
                 CommandName="NickSelectedCommand"
                 CommandParameter="{Binding NickName}" />
          </b:BehaviorCollection>
         </b:Interaction.Behaviors>
        </Button>
        
       </StackLayout>
      </StackLayout>

     </ViewCell>
    </DataTemplate>
   </ListView.ItemTemplate>
  </ListView>
  
  
 </StackLayout>
</ContentPage>

As you see we ‘linked’ CommandNameContext to the BindingContext associated to MainPage element through RelativeContext extension an we told EventToCommand that button’s Clicked event should invoke a Command named “NickSelectedCommad” also note that original BindingContext is preserved so we can safely use binding for the CommandParameter property.

Happy Behavioring… Smile

4 Responses to “Xamarin.Forms.Behaviors v.1.1.0”

  1. Hi Corrado, tried to use on my own project, no success, then, I downloaded the repository from git and the same error occurs.
    Any help? I also tried to add the reference on all 4 projects (PCL, Win, IOS and Droid), hint from this blog: http://blog.masterdevs.com/xf-day-7/ no success too

    The error is:
    System.IO.FileNotFoundException: Could not load file or assembly ‘Xamarin.Behaviors’ or one of its dependencies. The system cannot find the file specified.

    Tks

    Comment by Alexandre — 07/10/2014 @ 13:54

  2. Hi Alexandre, thanks for your feedback,issue has been fixed on version 1.2.0

    Comment by corcav — 08/10/2014 @ 10:23

  3. HI,

    Just a quick note to say thanks for adding RelativeContext. I’ve used it successfully and got rid of a lot of ugly code behind. I come from a WPF background and Xamarin.Forms.Behaviors is a great little library, I use it with MVVM Light.

    Comment by Taz — 06/02/2015 @ 15:08

  4. Just a follow up. Seems the issue with UWP project, is nuget wont add the reference to it. But the simple workaround solution is to just reference the DLL direct out of the nuget packages folder. That will resolve the 404, and works as expected. Thanks for the lib!

    Comment by Travis — 30/06/2016 @ 07:22

RSS feed for comments on this post. TrackBack URL

Leave a Response