是否可以获取标签的文本值?
例如,我点击“ Dessert”,我想获得特定的cat_code值,并将其传递给一个变量,稍后我将使用它
我的代码
Category.cs
public class Catergory
{
public string cat_code { get; set; }
}
MenuCategories.xaml
<ListView x:Name="MyCategory" ItemSelected="MyCategory_ItemSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label x:Name="categoryname" Text="{Binding cat_code}"
Style="{DynamicResource ListItemTextStyle}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我应该在这里使用什么活动?轻按或选择的项目? 以及如何获取cat_code的值或标签的文本?使用这些事件?
MenuCategories.xaml.cs
public MenuCategories()
{
InitializeComponent();
GetCategoryAsync();
}
public async Task GetCategoryAsync()
{
HttpClient client = new HttpClient();
var response = await client.GetStringAsync("http://ropenrom24-001-site1.etempurl.com/potangina/final/Restserver/index.php/category/view");
var cat = JsonConvert.DeserializeObject<List<Catergory>>(response);
MyCategory.ItemsSource = cat;
}
private void MyCategory_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
//what to code? to get value of the selecteditem
}
答案 0 :(得分:0)
e.SelectedItem
保存着选中的项目,因此代码可以简单地是:
private string selectedItem;
private void MyCategory_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var selectedCategory = e.SelectedItem as Catergory;
if (selectedCategory != null)
selectedItem = selectedCategory.cat_code;
}