所以我目前正在尝试为我的 Xamarin 表单应用程序提供自动完成功能。该应用程序将从 Google 的自动完成 API 获取输入,对其进行反序列化,并将其传递到一个表中,然后该表会将数据传递给 listvbiew...目前我已成功完成以下操作:
使“位置”条目接受输入并将其转换为 Google 自动完成 API 网址。
将请求发送到 Google 的 Autocomplete API,将请求转换为 JSON 包
- 将 JSON 反序列化到名为“results”的列表中
这里是负责上述内容的大部分代码:
protected async void GetInfo(){
if (CrossConnectivity.Current.IsConnected){
try{
HttpClient webSource = new HttpClient();
Activity_Indicator.IsRunning = true;
var content = await webSource.GetStringAsync(url);
string EditedJSON = "[" + content + "]";
var DeserializedJSON = JsonConvert.DeserializeObject<List<PlacesAPI>>(EditedJSON);
ObservableCollection < PlacesAPI > results = new ObservableCollection<PlacesAPI>(DeserializedJSON);
ListViewUI.ItemsSource = results;
}
catch(Exception ey){
Debug.WriteLine("" + ey);
}
}
}
从这里开始,我需要做的就是知道如何获取 results/[0]/predictions/([0],[1],[2]) 中找到的“描述”值
并将其绑定(bind)到我的 ListView 中,其设置如下:
<ListView x:Name="ListViewUI">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding ???}" TextColor="Black"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
当一切都说完并完成后,它将使用结果列表中的三个描述值填充 ListView 。
最佳答案
I want the first row to have the description value from results/[0]/predictions/[0], the second row to have the description value from results/[0]/predictions/[1], and the third row to have the description value from results/[0]/predictions/[2]
那么您只想将 results[0].predictions
用作您的 ItemsSource
ListViewUI.ItemsSource = results[0].predictions;
您的绑定(bind)将只是到 description
属性
<Label Text="{Binding description}" TextColor="Black"/>
关于c# - [Xamarin]如何将列表中的结果绑定(bind)到Listview中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51922188/