问题描述
您好我是编程新手,但目前我遇到xamarin表单不能将类型'system.threading.tasks.task>隐式转换为system.collections.generic.List
,因为我我试图在启动应用程序时使用全局变量来优化应用程序
,当我尝试将菜单项列表设置为将由其他页面访问的全局变量时,它给了我那个错误。我不知道如何解决这个问题,所以有人请帮助我
这是我的
App.cs
你没有做任何事情 async in getBeverageList(),因此您可以安全地将其签名更改为
public List< MenuItemModel> getBeverageList()
之后,您应该停下来几天,并了解async / await和TPL ...
Hi I am new to programming, but currently I encounter xamarin forms cannot implicitly convert type 'system.threading.tasks.task> to system.collections.generic.Listas I am trying to use global variable upon launching the app to optimized the appwhen 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; }Thanks!
解决方案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...
这篇关于xamarin表单不能将类型'system.threading.tasks.task< System.collections.generic list'隐式转换为system.collections.generic List的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!