Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent of html datalist in Xamarin.Forms, mechanism that would allow to select predefined values but also a free-text entry?

We are building a Xamarin forms app.

One of the fields is supposed to be a select where you can pick one of the predefined values or enter a free text value like into the text field.

in HTML one would solve it by using <datalist>

    <input list="browsers" name="browser" id="browser">
    
    <datalist id="browsers">
      <option value="Edge">
      <option value="Firefox">
      <option value="Chrome">
      <option value="Opera">
      <option value="Safari">
    </datalist>

Is there an equivalent of HTML datalist control in Xamarin.Forms, that would allow selection of values and also a free-text entry?

If there isn't one, how is this sort of functionality (selection of values and also a free-text) achieved in iOS and Android? as it does feel like a quite common scenario.

like image 730
Matas Vaitkevicius Avatar asked Mar 09 '21 08:03

Matas Vaitkevicius


People also ask

What is ItemsSource in Xamarin forms?

ItemsSource. A ListView is populated with data using the ItemsSource property, which can accept any collection implementing IEnumerable . The simplest way to populate a ListView involves using an array of strings: XAML Copy.

What is AutomationId in Xamarin forms?

What is AutomationId in Xamarin. Forms? AutomationId is a property of the Element class that gets or sets a string value that allows the automation framework to find and interact with elements using the value.

What is the name of the view used for single line text entry using Xamarin forms?

The Label view is used to display text. It can show multiple lines of text or a single line of text. Label can present text with multiple formatting options used in inline. The label view can wrap or truncate text when it can't fit on one line.

What are type arguments in Xamarin forms?

Type arguments are specified as a string, and are typically prefixed, such as sys:String and sys:Int32. Prefixing is required because the typical types of CLR generic constraints come from libraries that are not mapped to the default Xamarin.Forms namespace.

What is the translateextension in Xamarin forms?

The article explains how to use Xamarin.Forms XAML markup extensions to extend the power and flexibility of XAML by allowing element attributes to be set from sources other than literal text strings. The TranslateExtension allows users to handle multi-language support in XAML at runtime.

How do I define a generic class in Xamarin forms?

Defining generic classes in Xamarin.Forms XAML, with the x:TypeArguments directive, is unsupported. Type arguments are specified as a string, and are typically prefixed, such as sys:String and sys:Int32.

Which option should I use for the datalist element?

For the datalist element, it is suggested that the user select one of the options you've given, but he can actually enter anything he wants in the input. Edit 1: So which one you use depends upon your requirements. If the user must enter one of your choices, use the select element.


1 Answers

Answer

I recommend using the Syncfusion.Xamarin.SfAutoComplete NuGet Package.

Note: Syncfusion can be used for Free via its Community License.

I've put together a sample app demonstrating how to use Syncfusion.Xamarin.SfAutoComplete: https://github.com/brminnick/AutoCompleteSample

Walkthrough

To view the completed code from this walkthrough, visit https://github.com/brminnick/AutoCompleteSample

1. Install Syncfusion.Xamarin.SfAutoComplete NuGet Package

  1. In Visual Studio, add the Syncfusion.Xamarin.SfAutoComplete NuGet Package to your iOS project, Android project and .NET Standard Project (if applicable).

2. Initialize Syncfusion.Xamarin.SfAutoComplete on iOS

  1. In the iOS Project, open AppDelegate.cs
  2. In the AppDelegate.cs file, in AppDelegate.FinishedLaunching method, add new Syncfusion.SfAutoComplete.XForms.iOS.SfAutoCompleteRenderer();, like so:
[Register(nameof(AppDelegate))]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        new Syncfusion.SfAutoComplete.XForms.iOS.SfAutoCompleteRenderer();

        global::Xamarin.Forms.Forms.Init();
        LoadApplication(new App());

        return base.FinishedLaunching(app, options);
    }
}

3. Use Syncfusion.Xamarin.SfAutoComplete in Xamarin.Forms

Here is an example of a Xamarin.Forms app using Syncfusion.Xamarin.SfAutoComplete:

using System.Collections.Generic;
using Syncfusion.SfAutoComplete.XForms;
using Xamarin.Forms;

namespace AutoCompleteSample
{
    public class App : Application
    {
        public App() => MainPage = new AutoCompletePage();
    }

    class AutoCompletePage : ContentPage
    {
        public AutoCompletePage()
        {
            Content = new SfAutoComplete
            {
                HeightRequest = 40,
                AutoCompleteSource = new List<string>
                {
                    "Edge",
                    "Firefox",
                    "Chrome",
                    "Opera",
                    "Safari",
                }
            };
        }
    }
}

Demo

This GIF was generated using this completed sample app: https://github.com/brminnick/AutoCompleteSample

enter image description here

like image 81
Brandon Minnick Avatar answered Oct 19 '22 20:10

Brandon Minnick