Events in Xamarin Picker (SfPicker)

31 May 20213 minutes to read

Three events have been used for a picker when it is in the Dialog mode. They are,

  • Opened
  • Closing
  • Closed

Opened event

The Opened event occurs when the picker is opened.

Closing event

The Closing event raises when the picker gets closing. You can stop the picker close action by setting the e.cancel to true.

Closed event

The Closed event was raised after the picker is closed.

<?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:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:syncfusion="clr-namespace:Syncfusion.SfPicker.XForms;assembly=Syncfusion.SfPicker.XForms"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="PickerClosingEventSample.MainPage">
             
              <ContentPage.Content>
                         <syncfusion:SfPicker Opened="picker_Opened" Closed="picker_Closed" Closing="picker_Closing">
                         ...
                        </syncfusion:SfPicker>
              </ContentPage.Content>

</ContentPage>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace PickerClosingEventSample
  {   
   public partial class MainPage : ContentPage    
    {
        SfPicker picker = new SfPicker();

        public MainPage()
        {
         InitializeComponent();
         picker.Opened += picker_Opened;
         picker.Closing += picker_Closing;
         picker.Closed += picker_Closed;

        }

        private void picker_Opened(object sender, EventArgs e)
        {
          // handle the open action
        }

        private void Picker_Closing(object sender, Syncfusion.XForms.Core.CancelEventArgs e)
        {
           // stop the close action by setting the `e.cancel` to true.
        }

        private void picker_Closed(object sender, EventArgs e)
        {
           // hit after the picker is closed.
        }
    }
}

NOTE

View sample in GitHub

NOTE

You can refer to our Xamarin Picker feature tour page for its groundbreaking feature representations. You can also explore our Xamarin.Forms Picker example to knows the functionalities of each feature.