This section describes adding the AutoComplete control to your portable or shared application and displaying a list of items in the drop-down as suggestions for users.
Complete the following steps to display an AutoComplete control.
The following image shows how the AutoComplete control provides a list of suggestions in a drop-down list when the user enters text.
Complete the following steps to add a list of items to be displayed in the drop-down list.
C# |
Copy Code
|
---|---|
public class Country { public override string ToString() { return Name; } public static readonly string[] Countries = { "Germany", "Japan", "Australia", "Bangladesh", "Brazil", "Canada", "China", "France", "India", "Nepal", "Pakistan", "Srilanka" }; public string Name { get; set; } public static ObservableCollection<Country> GetCountries() { ObservableCollection<Country> listContries = new ObservableCollection<Country>(); foreach (var item in Countries) { listContries.Add(new Country() { Name = item }); } return listContries; } } |
XAML |
Copy Code
|
---|---|
xmlns:c1="clr-namespace:C1.Xamarin.Forms.Input;assembly=C1.Xamarin.Forms.Input" |
XAML |
Copy Code
|
---|---|
<StackLayout Orientation="Vertical"> <c1:C1AutoComplete x:Name="autoComplete" ItemsSource="{Binding ItemsSource}" DisplayMemberPath="Name" HorizontalOptions="FillAndExpand" VerticalOptions="Start"></c1:C1AutoComplete> </StackLayout> |
C# |
Copy Code
|
---|---|
autoComplete.ItemsSource = Country.GetCountries(); |
C# |
Copy Code
|
---|---|
public App() { // The root page of your application MainPage = new Page1(); } |
C# |
Copy Code
|
---|---|
C1.Xamarin.Forms.Input.Platform.iOS.C1InputRenderer.Init(); |
C# |
Copy Code
|
---|---|
C1.Xamarin.Forms.Input.Platform.UWP.C1InputRenderer.Init(); |
(Optional) In case you compile your UWP application in Release mode, you need to explicitly add the following code to the OnLaunched method in your App.xaml.cs to include the correct assemblies with your application.
C# |
Copy Code
|
---|---|
var assembliesToInclude = new List<Assembly>(); assembliesToInclude.Add(typeof(C1.Xamarin.Forms.Input.Platform.UWP.C1InputRenderer) .GetTypeInfo().Assembly); assembliesToInclude.Add(typeof(C1.UWP.Input.C1InputRenderer).GetTypeInfo().Assembly); Xamarin.Forms.Forms.Init(e, assembliesToInclude); |