c# - [Xamarin]如何将列表中的结果绑定(bind)到Listview中?

标签 c# xamarin

所以我目前正在尝试为我的 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);
            }
        }
    }

这给了我以下列表: /image/yMU78.png

从这里开始,我需要做的就是知道如何获取 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/

相关文章:

c# - 我可以强制子类实现接口(interface)吗?

c# - 我怎样才能阻止网站?

c# - 模拟用户

c# - 如何从 ASP.NET/Silverlight 开发人员成为 Sharepoint 开发人员?

c# - 如何逐渐旋转一个物体以面对另一个转动最短距离

c# - 显示有关未处理异常的对话框

ios - Xamarin iOS 推送通知在 iOS 13 中停止工作

c# - 是否可以在 Xamarin 表单内容页面和 Android Activity 之间导航?

c# - 无法从xamarin android调用firebase云函数

c# - 检查 AppDelegate 中的方法/属性是否存在