In Focus

Xamarin.Forms - ListView Context Actions

In this article, you will learn how to add a Context Action in ListView in Xamarin.Forms.

Introduction

 
Xamarin.Forms - ListView Context Actions
 
The Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. This means that reading and writing files is most easily done using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.
 
Prerequisites
  • Visual Studio 2017 or later (Windows or Mac)

Setting up a Xamarin.Forms Project

 
Start by creating a new Xamarin.Forms project. You’ll learn more by going through the steps yourself.
 
Visual Studio 2019 has more options in the opening window. Clone or check out the code from any repository or, open a project or solution for your computer.
 
Now, you need to click "Create a new project".
 
Xamarin.Forms - ListView Context Actions
 
Now, filter by Project Type: Mobile
 
Choose the Mobile App (Xamarin. forms) project under C# and Mobile.
 
Name your app. You probably want your project and solution to use the same name as your app. Put it on your preferred location for projects and click "Create".
 
Now, select the blank app and target platforms - Android, iOS and Windows (UWP).
 
Subsequently, go to the solution. In there, you get all the files and sources of your project (.NET Standard). Now, select the XAML page and double-click to open the MainPage.Xaml page.
 
You now have a basic Xamarin.Forms app. Click the Play button to try it out.
 
Context Actions
  1. <ListView.ItemTemplate>    
  2.                <DataTemplate>    
  3.                    <ViewCell>    
  4.                        <ViewCell.ContextActions>    
  5.                            <MenuItem Clicked="OnEdit" CommandParameter="{Binding Name}" Text="Edit"/>    
  6.                            <MenuItem Clicked="OnDelete" CommandParameter="{Binding Name}" Text="Delete" IsDestructive="True" />    
  7.                        </ViewCell.ContextActions>    
  8.                        <StackLayout Padding="15,0">    
  9.                            <Label Text="{Binding Name}" />    
  10.                        </StackLayout>    
  11.                    </ViewCell>    
  12.                </DataTemplate>    
  13.            </ListView.ItemTemplate>    
Context Action Events
  1. public void OnEdit(object sender, EventArgs e)  
  2. {  
  3.     var mi = ((MenuItem)sender);  
  4.     DisplayAlert("Edit", mi.CommandParameter.ToString(), "OK");  
  5. }  
  6.   
  7. public void OnDelete(object sender, EventArgs e)  
  8. {  
  9.     var mi = ((MenuItem)sender);  
  10.     DisplayAlert("Delete", mi.CommandParameter.ToString(), "OK");  
  11. }   

Setting up the User Interface

 
Go to MainPage.Xaml and write the following code.
 
MainPage.xaml
  1. <?xml version="1.0" encoding="utf-8" ?>    
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"    
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"    
  4.              xmlns:local="clr-namespace:XamarinList"    
  5.              x:Class="XamarinList.MainPage">    
  6.     
  7.     <StackLayout>    
  8.         <Image Margin="20,100,20,0" Source="banner1.png"/>    
  9.         <ListView x:Name="lstMonkeys">    
  10.             <ListView.ItemTemplate>    
  11.                 <DataTemplate>    
  12.                     <ViewCell>    
  13.                         <ViewCell.ContextActions>    
  14.                             <MenuItem Clicked="OnEdit" CommandParameter="{Binding Name}" Text="Edit"/>    
  15.                             <MenuItem Clicked="OnDelete" CommandParameter="{Binding Name}" Text="Delete" IsDestructive="True" />    
  16.                         </ViewCell.ContextActions>    
  17.                         <StackLayout Padding="15,0">    
  18.                             <Label Text="{Binding Name}" />    
  19.                         </StackLayout>    
  20.                     </ViewCell>    
  21.                 </DataTemplate>    
  22.             </ListView.ItemTemplate>    
  23.         </ListView>    
  24.     </StackLayout>    
  25.     
  26. </ContentPage>    
MainPage.Xaml.cs 
  1. using Xamarin.Forms;  
  2.   
  3. namespace XamarinList  
  4. {  
  5.     public partial class MainPage : ContentPage  
  6.     {  
  7.         string[] monkeys = new string[] { "Tamarins""Capuchins""Squirrel Monkeys""Spider Monkeys" };  
  8.         public MainPage()  
  9.         {  
  10.             InitializeComponent();  
  11.             List<MonkeyList> monkeyLists = new List<MonkeyList>();  
  12.             foreach (var monkey in monkeys)  
  13.             {  
  14.                 MonkeyList monkeyList = new MonkeyList() { Name= monkey };  
  15.                 monkeyLists.Add(monkeyList);  
  16.             }  
  17.             lstMonkeys.ItemsSource = monkeyLists;  
  18.         }  
  19.   
  20.         public void OnEdit(object sender, EventArgs e)  
  21.         {  
  22.             var mi = ((MenuItem)sender);  
  23.             DisplayAlert("Edit", mi.CommandParameter.ToString(), "OK");  
  24.         }  
  25.   
  26.         public void OnDelete(object sender, EventArgs e)  
  27.         {  
  28.             var mi = ((MenuItem)sender);  
  29.             DisplayAlert("Delete", mi.CommandParameter.ToString(), "OK");  
  30.         }  
  31.     }  
  32. }  
Click the "Play" button to try it out.
 
Xamarin.Forms - ListView Context Actions Xamarin.Forms - ListView Context Actions
Xamarin.Forms - ListView Context Actions
 
I hope you have understood how to add Context Actions in ListView in Xamarin.Forms.
 
Thanks for reading. Please share your comments and feedback. Happy Coding :)