xamarin forms cannot implicitly convert type '

2019-06-12 17:23发布

Hi I am new to programming, but currently I encounter xamarin forms cannot implicitly convert type 'system.threading.tasks.task> to system.collections.generic.List as I am trying to use global variable upon launching the app to optimized the app when I am trying to set the List of menu items into the global variable which will be access by the other pages, it gave me that error. I have no idea how to solve that issue so someone please help me

Here is my App.cs

private static int globalVariable = 1;
        public static List<MenuItemModel> foodList = new List<MenuItemModel>();
        private static List<MenuItemModel> beverageList = new List<MenuItemModel>();
        public static int GlobalVariable
        {
            get { return globalVariable; }
            set { globalVariable = value; }
        }
        public static List<MenuItemModel> FoodList
        {
            get { return foodList; }
            set { foodList = value; }
        }
        public static List<MenuItemModel> BeverageList
        {
            get { return beverageList; }
            set { beverageList = value; }
        }

        public App()
        {
            GlobalVariable = 10;

            BeverageList = getBeverageList();
            FoodList = getFoodList();
        }
public async Task<List<MenuItemModel>> getBeverageList()
        {
            ConstantCS constant = new ConstantCS();
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://172.20.129.44/");


            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


            HttpResponseMessage response = new HttpResponseMessage();
            response = client.GetAsync("WebServices/menu.svc/GetBeveragesJSON").Result;

            if (response.IsSuccessStatusCode)
            {
                string jsonString = await response.Content.ReadAsStringAsync();
                dynamic dynamicObject = JsonConvert.DeserializeObject(jsonString);

                int itemId_;
                string itemName_;
                string itemCategory_;
                string itemSubCategory_;
                string itemDescription_;
                string itemImage_;
                int itemQuantity_;
                double itemPrice_;
                string itemStatus_;
                string itemAddOn_;

                for (int i = 0; i < dynamicObject.d.Count; i++)
                {
                    itemId_ = dynamicObject.d[i]["itemID"];
                    itemName_ = dynamicObject.d[i]["itemName"].ToString();
                    itemCategory_ = dynamicObject.d[i]["itemCategory"].ToString();
                    itemSubCategory_ = dynamicObject.d[i]["itemSubCategory"].ToString();
                    itemDescription_ = dynamicObject.d[i]["itemDesc"].ToString();
                    itemImage_ = dynamicObject.d[i]["itemImg"].ToString();
                    itemQuantity_ = int.Parse(dynamicObject.d[i]["itemQty"].ToString());
                    itemPrice_ = double.Parse(dynamicObject.d[i]["itemPrice"].ToString());
                    itemStatus_ = dynamicObject.d[i]["itemStatus"].ToString();
                    itemAddOn_ = dynamicObject.d[i]["itemRequest"].ToString();

                    string itemURL_ = constant.PhotoBaseURL + itemImage_;



                    beverageList.Add(new MenuItemModel(itemId_, itemName_, itemCategory_, itemSubCategory_, itemDescription_, itemURL_, itemQuantity_, itemPrice_, itemStatus_, itemAddOn_));
                }

            }
            else
            {
                //Debug.WriteLine("It entered else not if");
            }
            return beverageList;
        }

The error that was shown Thanks!

1条回答
小情绪 Triste *
2楼-- · 2019-06-12 18:03

You're not doing anything async in getBeverageList(), so you can safely change its signature to

public List<MenuItemModel> getBeverageList()

After that, you should stop for a few days, and learn about async/await and TPL...

查看更多
登录 后发表回答