这是我的 .xml 文件 - listview 的一部分,其中标签从 API 获取数据。当我单击一项时,我需要获取其标题和/或日期的值。
ListView x:Name="listViewStories" Margin="12,0,12,0">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label x:Name="titleOfStory"
Text="{Binding Title}"
TextColor="Black"
FontSize="20"
WidthRequest="180">
</Label>
<Label x:Name="dateOfStory"
Text="{Binding Date}"
TextColor="Black"
FontSize="20"
WidthRequest="180"></Label>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
这是我的 .cs 文件,我仅将其用作 DisplayAlert 代码的示例,以便获取值,我尝试了不同的方法,但找不到解决方案:
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ReadYourPastPage : ContentPage
{
HttpClient client = new HttpClient();
string _ID;
public ReadYourPastPage(string id)
{
InitializeComponent();
_ID = id;
GetStories();
listViewStories.ItemTapped += ListViewStories_ItemTapped;
}
private void ListViewStories_ItemTapped(object sender, ItemTappedEventArgs e)
{
DisplayAlert("I need here to show Title or Date ->", e.Item.ToString() , "Cancel");
}
public async void GetStories()
{
var response = await client.GetStringAsync(returnJSON.GetURL() +
"index.php?IDuser=" + _ID +
"&getStories");
var stories = JsonConvert.DeserializeObject<List<Story>>(response);
listViewStories.ItemsSource = stories;
}
}
相关分类