当 ListView ItemTapped 时,如何使用 ListView 中的数据填充搜索栏字段(项目源)? XAMARIN 表格

How can I populate the Search bar field (Item Source) with data from the ListView when the ListView ItemTapped? XAMARIN FORMS

我创建了一个在文本更改时在列表视图中显示地址的条目。它运作良好。现在我想实现 ItemTaped 来填充条目以保存该数据(因为我是 saving/binding 条目结果)。我知道如何只对一个字符串(名称)执行此操作,但我的代码中有 2 个字符串(代码名称中的字符串搜索更改文本和字符串位置字符串格式化位置)。 下面是我的代码。所以我的问题是如何为 ItemTapped(searchResults_ItemTapped-in 代号)编写代码以便 ListView 数据填充我的输入字段(代号中的 locationText_Changed-)?谢谢

 private async void locationText_Changed(object sender, TextChangedEventArgs e)
    {
        try
        {           
        if (location == null)
        {
            await GetLocation();
        }
        string formattedLocation = string.Format("{0}%2C{1}", location.Latitude, location.Longitude);
        List<Prediction> predictionList = await VenueLogic1.GetVenues1(e.NewTextValue, formattedLocation);      
            searchResults.ItemsSource = predictionList;
            
        } catch (NullReferenceException nulrex)
        {
        } catch (Exception except)
        {
        }
    }
    private void searchResults_ItemTapped(object sender, ItemTappedEventArgs e)
    {
    }

UI:

<Entry x:Name="locationEntry"
                       TextChanged="locationText_Changed"
                       Placeholder="Unesi Lokaciju"/>                  />
                    <ListView x:Name="searchResults" HorizontalOptions="FillAndExpand" ItemTapped="searchResults_ItemTapped" >
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <Label Text="{Binding description}"/>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>

您通过名称引用了 locationEntry,您可以使用模式匹配进行类型检查并将 ItemTappedEventArgs.Item 转换为您的搜索结果类型,然后从该对象中获取描述。

private void searchResults_ItemTapped(object sender, ItemTappedEventArgs e)
{
    if (e.Item is MySearchResultClass result)
    {
        locationEntry.Text = result.description;
    }
}