首页 > 解决方案 > 设置 Listview 后 Xaml 分配额外的类

问题描述

简要说明:我正在忙的项目是使用名为 Woo-Commerece.Net 的 Woo-commerce API 的包装器,除了 Image 之外,我可以正常加载所有内容。从我的角度来看,我认为这是一个疏忽,我错过了一种方法做这个。

继承人模型:https ://github.com/XiaoFaye/WooCommerce.NET/blob/master/WooCommerce/v3/Product.cs#L503

生成列表时,我最终会迭代产品类。但是,图像存储在不同的模型中,我不知道如何在设置列表视图后为 Xaml 调用另一个类。

我只需要 1 个图像来传递我知道产品模型有一个名为图像的列表但我不知道你如何将列表传递给图像源绑定

我的 Xaml


<?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.Products"
             Title="Productsold"
             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="something" Text="{Binding name}" FontSize="Large"/>
                                        <Image Source="{Binding     }"/>
                                        <Label Text="{Binding date_created, StringFormat='{0:dd/MM/yyyy}'}" FontSize="Small" FontAttributes="Italic"/>
                                        <Label Text="{Binding price }"/>
                                        <Label Text="{x:Binding enable_html_description  }" FontSize="Medium"/>
                                        <Label Text="{x:Binding sku}" FontSize="Medium"/>
                                        <Button BindingContext="{Binding id}" Clicked="ProductClicked"></Button>
                                    </StackLayout>
                                </Frame>

                            </ViewCell.View>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

        </StackLayout>
    </ContentPage.Content>
</ContentPage>

后端

 public Products()
        {
            InitializeComponent();



            InitAsync();



        }
        private async void Back_Clicked(object sender, EventArgs e)
        {
            await Navigation.PopAsync();
        }



        private async Task InitAsync()
        {



            RestAPI rest = new RestAPI("http://example.co.za/wp-json/wc/v3/", "CK_XXXXXX", "cs_xXXXXX");
                WCObject wc = new WCObject(rest);
            var products = await wc.Product.GetAll();
            var p = await wc.Product.GetAll(new Dictionary<string, string>() {
                    {"tag", Suppliers.tagid },
                    { "per_page", "80" } }); ;

            List<ProductImage> z = new List<string> { p.images };
            productsListView.ItemsSource = p;

        }

        async void ProductClicked(object sender, EventArgs args)
        {





            var btn = (Button)sender;

           var a = btn.BindingContext;

            Orders.singleID = Convert.ToInt32(a);




           await Navigation.PushAsync(new Orders());

标签: c#.netxamlxamarinwoocommerce

解决方案


我发现没有办法按照我当时想要的方式来做这件事。

简短的问题是 Product 以 Image[] 为例,我需要访问

Product Image[].src 所以在绑定中我最后说

图片[我们索引你想要的图片是].src

以防万一您想动态检查该索引是什么您可以尝试这样的事情

所以如果你有初始化列表

例如


  var p = await wc.Product.GetAll(new Dictionary<string, string>() {
                    {"tag", Suppliers.tagid },
                    { "per_page", "80" } }); ;



  foreach (var item in p)
            {
                foreach (var z in item.images)
                {
                    if (item.images != null)
                    {
                        var YourBinding = z.src;
                    }
                }
            }

//There is more then likely a better way to go about it but its how I ended up sorting the problem for one of the Pull Methods of mine

推荐阅读