On typing in Combobox, I put a delay of 500ms OnValueChanged to call an API to search in the Database to fill the Datasource.
The DataSource is always good, but sometime, the combobox list doesn't refresh correctly. Sometime, the datasource is empty and the last list stay in the UI. How to be sure the list is always showing correctly ?
Here my code (note: The method LoadResultsAsync will call the API and will fill the ResultList (for the DataSource) with a NotifyPropertyChanged)
<comboBox:SfComboBox
x:Name="searchComboBox"
HeightRequest="40"
MaximumDropDownHeight="200"
IsEditableMode="true"
DataSource="{Binding ResultList}"
NoResultsFoundText="{local:Translate NoResultsFound}"
DropDownBackgroundColor="#D5D5D5"
SelectedDropDownItemColor="#D5D5D5"
ValueChanged="ComboBox_OnValueChanged"
Watermark="{local:Translate Search}"
WatermarkColor="DarkGray"
DisplayMemberPath="Name"
DropDownTextColor="Black"
SelectionChanged="SfComboBox_OnSelectionChanged">
<comboBox:SfComboBox.DropDownButtonSettings>
<comboBox:DropDownButtonSettings Width="40" Height="40">
<comboBox:DropDownButtonSettings.View>
<Label Style="{StaticResource SearchLabelStyle}" FontSize="18" Padding="0,10,0,0"
FontAttributes="Bold" TextColor="DarkGray" />
</comboBox:DropDownButtonSettings.View>
</comboBox:DropDownButtonSettings>
</comboBox:SfComboBox.DropDownButtonSettings>
</comboBox:SfComboBox>
private async void ComboBox_OnValueChanged(object sender, Syncfusion.XForms.ComboBox.ValueChangedEventArgs e)
{
this.cts?.Cancel();
this.cts = new CancellationTokenSource();
CancellationToken ctoken = this.cts.Token;
try
{
int millisDelay = this.TextChangedDelay > 0 ? this.TextChangedDelay : 500;
await Task.Delay(millisDelay, ctoken);
if (ctoken.IsCancellationRequested)
{
return;
}
try
{
if (string.IsNullOrEmpty(e.Value))
{
this.searchComboBox.DataSource = null;
}
else
{
if (e.Value.Length >= 3)
{
Device.BeginInvokeOnMainThread(
async () =>
{
await this.viewModel.LoadResultsAsync(e.Value);
});
}
}
}
catch (Exception ex)
{
}
}
catch (OperationCanceledException)
{
// Expected
}
}