Articles in this section
Category / Section

How to auto-populate tokens when control is loaded?

2 mins read

Step 1: Create an autocomplete control with a collection of names.

Step 2: Use the selected item property to auto-populate all the tokens when the control is loaded.

 

The following code demonstrates how to auto-populate tokens when Xamarin Autocomplete is loaded.

 

Code:

 

namespace SfAutoCompleteTokenSample
{
    public partial class MultiSelection : UIViewController
    {
        SfAutoComplete tokenAutoComplete;
        private readonly IList<string> cultureList = new List<string>();
        ObservableCollection<ContactsInfo> contactsInfoCollection;
        public MultiSelection() : base("MultiSelection", null)
        {
            //TokenAutoComplete
            tokenAutoComplete = new SfAutoComplete();
            tokenAutoComplete.MultiSelectMode = MultiSelectMode.Token;
            tokenAutoComplete.TokensWrapMode = TokensWrapMode.Wrap;
            contactsInfoCollection = new ContactsInfoCollection().GetContactDetails();
            tokenAutoComplete.DataSource = contactsInfoCollection;
            tokenAutoComplete.DisplayMemberPath = (NSString)"ContactName";
            tokenAutoComplete.ImageMemberPath = "ContactImage";
            tokenAutoComplete.ItemHeight = 60;
            tokenAutoComplete.SuggestionMode = SFAutoCompleteSuggestionMode.SFAutoCompleteSuggestionModeStartsWith;
            tokenAutoComplete.DropDownItemChanged += NativeAutoComplete_DropDownItemChanged;
            tokenAutoComplete.SelectedItem = new ContactsInfoCollection().GetContactDetails();
            this.Add(tokenAutoComplete);
            mainPageDesign();
        }
 
        UIView NativeAutoComplete_DropDownItemChanged(object sender, DropDownItemEventArgs e)
        {
            UIView parentView = new UIView();
            SfAutoComplete auto = (sender as SfAutoComplete);
            parentView.Frame = new CGRect(0, 0, auto.Bounds.Width, auto.ItemHeight);
            UILabel titleLabel = new UILabel();
            titleLabel.Frame = new CGRect(30, 5, auto.Bounds.Width - 65, auto.ItemHeight / 2 - 5);
            titleLabel.TextAlignment = UITextAlignment.Left;
         
            var item = auto.DataSource.ElementAt((int)e.Index);
            var selectedObject = (item as ContactsInfo);
 
            titleLabel.Text = selectedObject.ContactName;
 
            parentView.Add(titleLabel);
 
            e.View = parentView;
 
            return e.View;
        }
 
        public void mainPageDesign()
        {
            tokenAutoComplete.Frame = new CGRect(50, 110, UIScreen.MainScreen.Bounds.Width - 60, 100);
 
        }
 
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();
 
            // Perform any additional setup after loading the view, typically from a nib.
        }
 
        public override void DidReceiveMemoryWarning()
        {
            base.DidReceiveMemoryWarning();
            // Release any cached data, images, etc that aren't in use.
        }
    }
    public class ContactsInfo
    {
        #region Fields
 
        private string contactName;
 
        #endregion
 
        #region Constructor
 
        public ContactsInfo()
        {
 
        }
 
        #endregion
 
        #region Public Properties
 
        public string ContactName
        {
            get { return this.contactName; }
            set
            {
                this.contactName = value;
 
            }
        }
 
       
        #endregion
 
 
    }
    public class ContactsInfoCollection
    {
        #region Fields
 
        private Random random = new Random();
 
        #endregion
 
        #region Constructor
 
        public ContactsInfoCollection()
        {
 
        }
 
        #endregion
 
        #region Get Contacts Details
 
        public ObservableCollection<ContactsInfo> GetContactDetails()
        {
            ObservableCollection<ContactsInfo> customerDetails = new ObservableCollection<ContactsInfo>();
 
            for (int i = 0; i < CustomerNames.Count(); i++)
            {
                var details = new ContactsInfo()
                {
                    ContactName = CustomerNames[i],
 
                };
                customerDetails.Add(details);
            }
            return customerDetails;
        }
 
        #endregion
 
        #region Contacts Information
 
        string[] CustomerNames = new string[]
        {
            "Vegeta",
            "Trunks",
            "Goku",
            "Goten",
            "Gohan"
 
        };
        #endregion
    }
}
 

 

 

The following screenshot illustrates auto-populating the tokens when control is loaded.

Auto-populating in Xamarin Autocomplete

 

Please find the sample from the below link:

https://www.syncfusion.com/downloads/support/directtrac/general/ze/SfAutoCompleteTokenSample-378384412

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied