예제 #1
0
        private async void CountrySearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            _cancellationTokenSource?.Cancel();

            _cancellationTokenSource = new CancellationTokenSource();
            var cancellationToken = _cancellationTokenSource.Token;

            try
            {
                await Task.Delay(500, cancellationToken);

                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                var countries = await Task.Run(async() =>
                                               await FlagService.GetFlagsTask(e.NewTextValue), cancellationToken);

                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                Device.BeginInvokeOnMainThread(() =>
                                               Countries.ReplaceRange(countries));
            }
            catch (OperationCanceledException)
            {
                // we cancelled so do nothing... (ugly!)
            }
        }
예제 #2
0
        private async void CountrySearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            await Task.Delay(500);

            var countries = await FlagService.GetFlagsTask(e.NewTextValue);

            Device.BeginInvokeOnMainThread(() =>
                                           Countries.ReplaceRange(countries));
        }
예제 #3
0
        //Source: https://byteloom.marek-mierzwa.com/mobile/2018/06/18/search-as-you-type-in-xamarin-forms.html

        private async void CountrySearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            _cancellationTokenSource?.Cancel();

            if (e.NewTextValue.Length < 3 && searchTerm == e.NewTextValue)
            {
                return;
            }

            _cancellationTokenSource = new CancellationTokenSource();
            var cancellationToken = _cancellationTokenSource.Token;

            try
            {
                await Task.Delay(500, cancellationToken);

                searchTerm = e.NewTextValue;

                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                var timeoutTask   = Task.Delay(TimeSpan.FromSeconds(30), cancellationToken);
                var countriesTask = Task.Run(async() => await FlagService.GetFlagsTask(e.NewTextValue),
                                             cancellationToken);

                if (await Task.WhenAny(countriesTask, timeoutTask) == timeoutTask)
                {
                    throw new TimeoutException();
                }

                if (cancellationToken.IsCancellationRequested)
                {
                    return;
                }

                var countries = await countriesTask;

                Device.BeginInvokeOnMainThread(() =>
                                               Countries.ReplaceRange(countries));
            }
            catch (OperationCanceledException)
            {
                // we cancelled so do nothing... (ugly!)
            }
            catch (TimeoutException timeoutEx)
            {
                //Handle timeout here...
                Console.WriteLine("Timeout!");
            }
        }