首页 > 解决方案 > 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 并从标签为正确加载它们

对此有任何建议或可能的解决方法吗?

期望的结果:单击供应商处的按钮,它会加载该标签下的每个产品。

标签: c#xamlxamarinwoocommerce

解决方案


按钮的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
}

推荐阅读