c# - Xamarin 从数据模板中的标签中提取文本(列表视图)
问题描述
我想从标签 id 中获取文本以在单击供应商时查询新负载
private async Task InitAsync()
{
RestAPI rest = new RestAPI("http://azipit.co.za/mica-market-app/wp-json/wc/v3/", "ck_0112f135e2fxxxxxxd621f919bc890", "cs_38ea21f4d63xxxxxxxxxx1868993b66065dcb0362fa");
WCObject wc = new WCObject(rest);
var products = await wc.Tag.GetAll();
var p = await wc.Tag.GetAll(new Dictionary<string, string>() {
{ "per_page", "80" } }); ;
productsListView.ItemsSource = p;
}
async void retrieveid()
{
}
async void SupplierClicked(object sender, EventArgs args)
{
RestAPI rest = new RestAPI("http://azipit.co.za/mica-market-app/wp-json/wc/v3/", "ck_0112f135e2f9b32cc14xxxxxxxxxxxx919bc890", "cs_38ea21f4d63exxxxx01868993b66065dcb0362fa");
WCObject wc = new WCObject(rest);
var products = await wc.Product.GetAll();
var p = await wc.Product.GetAll(new Dictionary<string, string>() {
{"tag" , tagid },
{ "per_page", "80" } }); ;
productsListView.ItemsSource = p;
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Ecombeta.Views.Suppliers"
Title="Suppliers"
BackgroundColor="#c4c3c0">
<ContentPage.Content>
<StackLayout>
<ListView x:Name="productsListView"
HasUnevenRows="True"
VerticalOptions="FillAndExpand"
SeparatorVisibility="None">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Frame HasShadow="True" Margin="8">
<StackLayout>
<Label x:Name="idlabel" Text="{Binding id}"></Label>
<Label Text="{Binding name}" FontSize="Large"/>
<Image Source="{Binding ProductImage.src}"/>
<Button Text="See Products"
VerticalOptions="CenterAndExpand"
HorizontalOptions="Center"
Clicked="SupplierClicked" />
</StackLayout>
</Frame>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
我不知道如何从“标签”中获取 ID,在这种情况下这是我的供应商,以便我可以查询以加载它下面的产品,所以我的逻辑是尝试使用 ax:Name 并从标签为正确加载它们
对此有任何建议或可能的解决方法吗?
期望的结果:单击供应商处的按钮,它会加载该标签下的每个产品。
解决方案
按钮的BindingContext
将是对该行相关项目的引用
protected void SupplierClicked(object sender, EventArgs a)
{
var btn = (Button)sender;
var product = (Product)btn.BindingContext;
// now you can reference any property of Product that you need
}
推荐阅读
- arrays - 无法遍历 Bash 字符串数组
- visual-studio - 使用 Visual Studio Comm 2019 创建 BI Cube。向导欢迎屏幕不会消失,因此无法输入信息
- css - sass 文件编译失败
- php - 如何在 Laravel 中对多对多关系集合的总价格求和
- python - 创建一个程序,计算列表 object_list 中有多少对象与列表中的其他元素具有相同的哈希值
- java - 块纹理已成功加载,但“BlockItem”纹理未加载,Minecraft Forge 1.15.2
- filter - primefaces dataTable filterBy 不调用 load 方法
- java - Android Studio 尝试在空对象引用上调用虚拟方法“int java.util.ArrayList.size()”
- optimization - 优化求和函数 - GEKKO
- python - Scrapy 在不“更深”的情况下返回正确的数据,但在“更深”时返回重复数据