Back to Devexpress

AsyncItemsSourceProvider Class

mobilecontrols-devexpress-dot-xamarinforms-dot-editors-681fedb5.md

latest5.7 KB
Original Source

AsyncItemsSourceProvider Class

A data provider that supplies suggestions for the AutoCompleteEdit and AutoCompleteColumn in async mode.

Namespace : DevExpress.XamarinForms.Editors

Assembly : DevExpress.XamarinForms.Editors.dll

NuGet Package : DevExpress.XamarinForms.Editors

Declaration

csharp
public class AsyncItemsSourceProvider :
    ItemsSourceProviderBase

Remarks

Attach the AsyncItemsSourceProvider to the editor and handle its SuggestionsRequested event. Use the Request event argument to specify the method that returns suggestions. The provider assigns the returned collection to the ItemsSource property. To get the text entered in the edit box and pass it to the specified method, use the Text event argument.

You can also specify the following options:

  • RequestDelay — the time that should elapse after the text is changed and the request is called. Use this parameter to reduce the number of requests as a user types.
  • CharacterCountThreshold — the number of entered characters after which the provider starts to make requests. For example, the provider can make requests only if a user enters at least three characters.

The provider cancels the previous request if a new request is submitted. You can use the CancellationToken event argument to cancel the previous request.

Examples

The example below uses the AsyncItemsSourceProvider to supply suggestions for the AutoCompleteEdit.

xml
<dxe:AutoCompleteEdit
    LabelText="State"
    PlaceholderText="Type here to search..."
    VerticalOptions="Center"
    Margin="16,0">

    <dxe:AutoCompleteEdit.ItemsSourceProvider>
        <dxe:AsyncItemsSourceProvider SuggestionsRequested="OnDelegateRequested" />
    </dxe:AutoCompleteEdit.ItemsSourceProvider>
</dxe:AutoCompleteEdit>
cs
using System.Collections.Generic;
using System.ComponentModel;
using Xamarin.Forms;

namespace AutoCompleteEditExample {
    public partial class MainPage : ContentPage {
        public MainPage() {
            InitializeComponent();
            States = new List<string>();
            States.AddRange(new string[] { "California", "Colorado", "Connecticut" /*...*/ });
        }
        public List<string> States { get; }
        void OnDelegateRequested(object sender, SuggestionsRequestEventArgs e) {
            e.Request = () => {
                return States.Where(i => i.StartsWith(e.Text, StringComparison.CurrentCultureIgnoreCase)).ToList();
            };
        }
    }
}

The example below uses the AsyncItemsSourceProvider to supply suggestions for the AutoCompleteColumn.

xml
<dxg:DataGridView ItemsSource="{Binding Path=Employees}">
    <dxg:DataGridView.Columns>
        <dxg:AutoCompleteColumn FieldName="JobTitle">
            <dxg:AutoCompleteColumn.ItemsSourceProvider>
                <dxe:AsyncItemsSourceProvider SuggestionsRequested="SuggestionsRequested"
                                              RequestDelay="500"
                                              CharacterCountThreshold="2"/>
            </dxg:AutoCompleteColumn.ItemsSourceProvider>                
        </dxg:AutoCompleteColumn>
    </dxg:DataGridView.Columns>
</dxg:DataGridView>
cs
using System.Collections.Generic;
using System.ComponentModel;
using Xamarin.Forms;

namespace DemoCenter.Forms.Views {
    public partial class FirstLookView : ContentPage {
        public FirstLookView() {
            InitializeComponent();
            BindingContext = new EmployeesRepository();
        }

        private void SuggestionsRequested(object sender, SuggestionsRequestEventArgs e) {
            EmployeesRepository employeesRepository = BindingContext as EmployeesRepository;
            e.Request = () => {
                return employeesRepository.JobTitles.Where(i => i.StartsWith(e.Text, StringComparison.CurrentCultureIgnoreCase)).ToList();
            };
        }
    }
    public class EmployeesRepository {
        public IList<Employee> Employees { get; set; }
        public IList<string> JobTitles { get; set; }
    }
    public class Employee {
        public string JobTitle { get; set; }
    }
}

Inheritance

Object ItemsSourceProviderBase AsyncItemsSourceProvider

See Also

AsyncItemsSourceProvider Members

AutoCompleteEdit

DevExpress.XamarinForms.Editors Namespace