Skip to content

Instantly share code, notes, and snippets.

@motamed
Last active January 25, 2023 11:30
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motamed/4ef5b0b9c7eb57f927d935327d04f937 to your computer and use it in GitHub Desktop.
Save motamed/4ef5b0b9c7eb57f927d935327d04f937 to your computer and use it in GitHub Desktop.
How display Action Sheet in Xamarin.Forms

How display Action Sheet in Xamarin.Forms

 DisplayActionSheet("Title","Cancel","Destruction","Button1","Button2","Button3",...);

The UIActionSheet is a common UI element in iOS. The Xamarin.Forms DisplayActionSheet method lets you include this control in cross-platforms apps, rendering native alternatives in Android and Windows Phone.

To display an action sheet, await DisplayActionSheet in any Page, passing the message and button labels as strings. The method returns the string label of the button that was clicked by the user. A simple example is shown here:

        private async void displayActionSheetBtn_Clicked(object sender, EventArgs e)
        {
            var actionSheet = await DisplayActionSheet("Title","Cancel", null, "Button1","Button2","Button3");

            switch (actionSheet)
            {
                case "Cancel":
                
                    // Do Something when 'Cancel' Button is pressed
                
                    break;

                case "Button1":

                    // Do Something when 'Button1' Button is pressed

                    break;

                case "Button2":

                    // Do Something when 'Button2' Button is pressed

                    break;
                case "Button3":

                    // Do Something when 'Button3' Button is pressed

                    break;

            }
            
        }




The destroy button is rendered differently than the others, and can be left null or specified as the third string parameter. The following example uses the destroy button:

 var actionSheet = await DisplayActionSheet("Title","Cancel","Destruction","Button1","Button2","Button3");

            switch (actionSheet)
            {
                case "Cancel":
                
                    // Do Something when 'Cancel' Button is pressed
                
                    break;

                case "Destruction":

                    // Do Something when 'Destruction' Button is pressed

                                        .
                                        .
                                        .                    




Complete Code

MainPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:DisplayActionSheet"
             x:Class="DisplayActionSheet.MainPage">


    <StackLayout VerticalOptions="Center">
        <Button Text="Display Action Sheet" x:Name="displayActionSheetBtn" Clicked="displayActionSheetBtn_Clicked"></Button>
    </StackLayout>

</ContentPage>


MainPage.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace DisplayActionSheet
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private async void displayActionSheetBtn_Clicked(object sender, EventArgs e)
        {
            var actionSheet = await DisplayActionSheet("Title","Cancel","Destruction","Button1","Button2","Button3");

            switch (actionSheet)
            {
                case "Cancel":
                
                    // Do Something when 'Cancel' Button is pressed
                
                    break;

                case "Destruction":

                    // Do Something when 'Destruction' Button is pressed

                    break;
                case "Button1":

                    // Do Something when 'Button1' Button is pressed

                    break;

                case "Button2":

                    // Do Something when 'Button2' Button is pressed

                    break;
                case "Button3":

                    // Do Something when 'Button3' Button is pressed

                    break;

            }
            
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment