Sunday, April 3, 2016

Floating Action Button in Xamarin Forms

I'm loving experimenting with Xamarin Forms at the moment. So when I wanted to include a Floating Action Button (FAB) in Android versions of a Forms App, I naturally wanted to try to get this working with Forms first.  I realise you can use Xamarin Native Android views in a Forms App, but I enjoy XAML and creating reusable controls.

My first port of call was James Montemagno's GitHub and blog and found his examples for a FAB.
He does mention he ported this code from:

This looks perfect, but I wanted to improve the Forms example to be mostly XAML based and try to best encapsulate the events into the FAB control to minimise the amount of work the consuming developer has to do.

Here's my code so far.

Its far from perfect, but works great.  Be aware however, that if your ObservableCollection has duplicate values in it, the FAB will exhibit unexpected hide/show behaviour.

Here's what I ended up with in my XAML:

  <AbsoluteLayout VerticalOptions="FillAndExpand"  
     <StackLayout AbsoluteLayout.LayoutFlags="All"  
       <Label Text="FAB Test 1"  
           HorizontalOptions="Center" />  
       <ListView x:Name="MainList"   
            ItemsSource="{Binding MyItems}"  
     <fabTest1:FloatingActionButtonView x:Name="Fab"   
                       Command="{Binding FabExecuteCommand}"  
                       ParentList="{Binding Source={x:Reference MainList}, Path=.}"/>  

No comments:

Post a Comment