首页 > 解决方案 > 抑制状态错误 CS0266 无法将类型“”隐式转换为“System.Collections.IEnumerable”

问题描述

嗨,我正在尝试提取单个产品而不是多个产品我发现 Wrapper WoocomNet 有一个 get 而不是 GetAll 方法,所以我传递了 ID 并尝试加载它,我得到了正确的 id 但无法将它加载到我的列表视图中向我的代码显示 XAML 和包装器方法


  async void ProductClicked(object sender, EventArgs args)
        {

            var btn = (Button)sender;
            var productid = btn.BindingContext;

            RestAPI rest = new RestAPI("site.com/wp-json/wc/v3/""key""secret")
             WCObject wc = new WCObject(rest);
            var p = await wc.Product.Get(Convert.ToInt32(productid));


            productsListView.ItemsSource = p;
}

Xaml


 <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 ProductImage.src}"/>
                                        <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>

包装器



namespace WooCommerceNET.Base
{
    public class WCItem<T>
    {
        public WCItem(RestAPI api);

        public string APIEndpoint { get; protected set; }
        public RestAPI API { get; protected set; }

        [AsyncStateMachine(typeof(WCItem<>.<Add>d__12))]
        public Task<T> Add(T item, Dictionary<string, string> parms = null);
        [AsyncStateMachine(typeof(WCItem<>.<AddRange>d__13))]
        public Task<BatchObject<T>> AddRange(BatchObject<T> items, Dictionary<string, string> parms = null);
        [AsyncStateMachine(typeof(WCItem<>.<Delete>d__17))]
        public Task<T> Delete(int id, bool force = false, Dictionary<string, string> parms = null);
        [AsyncStateMachine(typeof(WCItem<>.<DeleteRange>d__18))]
        public Task<string> DeleteRange(BatchObject<T> items, Dictionary<string, string> parms = null);
        [AsyncStateMachine(typeof(WCItem<>.<Get>d__9))]
        public Task<T> Get(int id, Dictionary<string, string> parms = null);
        [AsyncStateMachine(typeof(WCItem<>.<Get>d__10))]
        public Task<T> Get(string email, Dictionary<string, string> parms = null);
        [AsyncStateMachine(typeof(WCItem<>.<GetAll>d__11))]
        public Task<List<T>> GetAll(Dictionary<string, string> parms = null);
        [AsyncStateMachine(typeof(WCItem<>.<Update>d__14))]
        public Task<T> Update(int id, T item, Dictionary<string, string> parms = null);
        [AsyncStateMachine(typeof(WCItem<>.<UpdateRange>d__16))]
        public Task<BatchObject<T>> UpdateRange(BatchObject<T> items, Dictionary<string, string> parms = null);
        [AsyncStateMachine(typeof(WCItem<>.<UpdateWithNull>d__15))]
        public Task<T> UpdateWithNull(int id, object item, Dictionary<string, string> parms = null);
    }
}

我的错误代码:严重性代码描述项目文件行抑制状态抑制状态错误 CS0266 无法将类型“WooCommerceNET.WooCommerce.v3.Product”隐式转换为“System.Collections.IEnumerable”。存在显式转换(您是否缺少演员表?) Ecombeta \Products.xaml.cs 68 Active

我只需要将 productlistview 的源设置为 p 但我不确定这些演员是如何工作的

如果有人愿意借一分钟,我将不胜感激。

谢谢

对于 Ref 也将展示一路走来


 private async Task InitAsync()
        {




            RestAPI rest = new RestAPI("http://example.com/wp-json/wc/v3/", "ck_xxxx", "cs_xxxxxxx");
            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" } }); ;

                productsListView.ItemsSource = p;

        }

标签: c#xamarinwoocommerce

解决方案


ItemsSource只能设置为,IEnumerable即您必须创建产品集合:

productsListView.ItemsSource = new Product[1] { p };

推荐阅读