How do I use DropDownList in Xamarin?
3 minute(s) read
|
Published on: Oct 03, 2021
Updated on: Dec 14, 2021
|
How can we use Dropdown in Xamarin.Forms?
As we said before, rejecting the Android operating system is a slider control called Spinner. In other words, Xamarin. Forms Control is a special package that can be used for the platform. If we want to tell you better, let's give an example: Xamarin. Forms Entry is an EditText package that can be used in applications developed for the Android operating system ( in Slovak: aplikácie vyvinuté pre operačný systém Android )and platform, as well as UITextField in iOS.
The steps for creating a Dropdown slider are as follows:
- I must first create a new project in Xamarin.Forms.
- Then, we must create a Dropdown view in Xamarin.Forms .NET Standard Project to make the relevant settings after that.
- Next, in the Android project, to control Dropdown, we must turn on Spinner.
- Then, we must implement the settings related to it and simulate and test the trial version for the Android platform.
1- In the first step, we must create a project related to Xamarin.Forms. To do this, go to New - Project - Xamarin Cross-Platform App and then click OK to confirm.
2- Then, I select Android and iOS platforms.
3- The dropdown view should be selected in Xamarin.Forms .NET.
4- To do this, I created a class called Dropdown. Of course, you must note that you inherit Dropdown with View. If it does, it means that Dropdown is the child of Dropdown.
5- In the next steps, we must create all the features that can be connected.
ItemsSource: This feature is used to assign a list of data that must be placed in Dropdown and filled.
- SelectedIndex:
This feature can also identify a list of values selected from SourceSource items.
- ItemSelected:
This feature can also be used to perform an action when an item is selected in Dropdown.
6- Like the following, we need to create a connectable property for ItemsSource.
7- Next, we need to create a connectable property for SelectedIndex.
8- In the next steps, we need to create a custom ItemSelected event for dropdown control and event calling.
public event EventHandler ItemSelected.
public void OnItemSelected (int pos)
{
ItemSelected? .Invoke (this, new ItemSelectedEventArgs () {SelectedIndex = pos}).
}
9- As shown below, ItemSelectedEventArgs is a child of EventArgs.
10- Below is all the complete code that has been used to create the Dropdown.
11- Then, we must be able to package Spinner in the Android project and platform for Dropdown control.
12- To do this, we need to create a class file in the Android project called DropdownRenderer. Then add View Renderer.
13- After that, we must cancel OnElementChanged and OnElementPropertyChanged.
14- In ViewRenderer, we must configure Native Control using the SetNativeControl () method, as shown below.
15- Then, we must move from Xamarin.Forms Dropdown to Android Spinner control.
16- Select the default selection from the list of selected items.
17- We need to create an event for the selected item for the Spinner.
18- After creating the event required for the Dropdown, call.
19- By changing OnElementPropertyChanged, we must assign ItemsSource & SelectedIndex to Android Spinner.
20- To link the dropdown view in the standard .NET project to the Export project, add the Android Client to the top of the namespace.
21- The complete code related to the steps listed above to create a Dropdown Renderer is as follows:
22- In the final stages, we should implement Dropdown and create a test version for the Android platform and operating system.
In these sections, we must use View in the Xamarin.Forms section.
23- We must open the designer file in MainPage.xaml.
24- You must set ItemsSource and SelectedIndex.
25- We need to add the selection event for the Dropdown.
26- The complete codes related to all the above steps are as follows: