5using System.Threading.Tasks;
6using Microsoft.Maui.Controls;
14 private readonly TaskCompletionSource<ISO_3166_Country?> result =
new(TaskCreationOptions.RunContinuationsAsynchronously);
15 private CancellationTokenSource? cancellationTokenSource;
16 private bool isDisposed;
18 public Task<ISO_3166_Country?> Result => this.result.Task;
24 this.InitializeComponent();
25 this.BindingContext =
this;
26 this.InnerSearchBar.Text =
string.Empty;
29 private void SearchBar_TextChanged(
object? sender, TextChangedEventArgs e)
31 if (
string.IsNullOrEmpty(e.NewTextValue))
33 this.InnerListView.ItemsSource = Countries;
37 this.cancellationTokenSource?.Cancel();
38 this.cancellationTokenSource =
new CancellationTokenSource();
39 CancellationToken token = this.cancellationTokenSource.Token;
43 IEnumerable<ISO_3166_Country> filtered = Countries.Where(country =>
44 country.Name.Contains(e.NewTextValue, StringComparison.OrdinalIgnoreCase)
45 ||
string.Equals(country.Alpha2, e.NewTextValue, StringComparison.OrdinalIgnoreCase)
46 ||
string.Equals(country.Alpha3, e.NewTextValue, StringComparison.OrdinalIgnoreCase)
47 || country.DialCode.Contains(e.NewTextValue, StringComparison.OrdinalIgnoreCase));
49 this.Dispatcher.Dispatch(() =>
51 if (!token.IsCancellationRequested)
53 this.InnerListView.ItemsSource = filtered;
59 private async
void InnerListView_SelectionChanged(
object? sender, SelectionChangedEventArgs e)
63 this.result.TrySetResult(selected);
70 this.result.TrySetResult(
null);
71 await base.OnDisappearingAsync();
74 protected virtual void Dispose(
bool disposing)
80 this.cancellationTokenSource?.Dispose();
81 this.cancellationTokenSource =
null;
84 this.isDisposed =
true;
91 GC.SuppressFinalize(
this);
Conversion between Country Names and ISO-3166-1 country codes.
static ISO_3166_Country[] Countries
This collection built from Wikipedia entry on ISO3166-1 on 9th Feb 2016
Base class that references services in the app.
static IPopupService PopupService
Popup service for presenting application popups.
class ISO_3166_Country(string Name, string Alpha2, string Alpha3, int NumericCode, string DialCode, EmojiInfo? EmojiInfo=null)
Representation of an ISO3166-1 Country