我創建了一個條目,在文本更改時在串列視圖中顯示地址。它運作良好。現在我想實作 ItemTaped 來填充 Entry 以保存該資料(因為我正在保存/系結 Entry 結果)。我只知道如何對一個字串(名稱)執行此操作,但是我的代碼中有 2 個字串(字串搜索更改文本和字串位置字串 formatedLocation 代碼名稱)。下面是我的代碼。所以我的問題是如何為 ItemTapped(searchResults_ItemTapped-in code name) 撰寫代碼以便 ListView 資料填充我的 Entry 欄位(locationText_Changed-in code name)?謝謝。
private async void locationText_Changed(object sender, TextChangedEventArgs e)
{
try
{
if (location == null)
{
await GetLocation();
}
string formattedLocation = string.Format("{0},{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)
{
}
用戶界面:
<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>
uj5u.com熱心網友回復:
您有一個locationEntry按名稱的參考,您可以使用模式匹配進行型別檢查并轉換ItemTappedEventArgs.Item為您的搜索結果型別,然后從該物件中獲取描述。
private void searchResults_ItemTapped(object sender, ItemTappedEventArgs e)
{
if (e.Item is MySearchResultClass result)
{
locationEntry.Text = result.description;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/313143.html
標籤:列表显示 沙马林 xamarin.forms
