Xamarin.Forms无法隐式转换类型'system.threading.tasks.task>到system.collections.generic.List

时间:2016-11-16 12:55:07

标签: xamarin.forms

您好我是编程新手,但目前我遇到的xamarin表单无法隐式转换类型'system.threading.tasks.task> to system.collections.generic.List因为我试图在启动应用程序时使用全局变量来优化应用程序当我尝试将菜单项列表设置为将被其他页面访问的全局变量时,它给出了我那个错误。我不知道如何解决这个问题,所以有人请帮助我

这是我的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;
}

显示的错误

1 个答案:

答案 0 :(得分:3)

在构造函数中扭曲一个额外的异步方法:

    {
      "apiVersion": "2015-06-15",
      "dependsOn": [
        "[concat('Microsoft.Compute/virtualMachines/', variables('vm_name'))]"
      ],
      "location": "[resourceGroup().location]",
      "name": "DSCSetup",
      "properties": {
        "publisher": "Microsoft.Powershell",
        "type": "DSC",
        "typeHandlerVersion": "2.20",
        "autoUpgradeMinorVersion": true,
        "settings": {
          "modulesUrl": "[concat('https://', variables('sa_name'), '.blob.core.windows.net/.../dsc.ps1.zip')]",
          "configurationFunction": "dsc.ps1\\Main",
          "properties": {
            "MachineName": "[variables('vm_name')]",
            "UserName": "[parameters('vm_user')]"
          }
        },
        "protectedSettings": {}
      },
      "type": "extensions"
    }

现在您可以public App() { Initialize(); //no need to await this } public async Task Initialize() { GlobalVariable = 10; BeverageList = await getBeverageList(); //use await here! FoodList = await getFoodList(); //use await here! } awaitgetFoodList()的结果。

否则返回getBeverageList()本身。这会导致您的错误:

  

无法将类型'System.Threading.Tasks.Task'隐式转换为   “System.Collections.Generic.List&LT; MenuItemModel&gt;