2016-11-16 96 views
-1

嗨我是新手編程,但目前我遇到的xamarin表單不能隱式轉換類型'system.threading.tasks .task>到system.collections.generic.List,因爲我試圖在啓動應用程序時使用全局變量來優化應用程序,當我嘗試將菜單項列表設置爲將由其他頁面訪問的全局變量,它給了我那個錯誤。我不知道如何解決這個問題,所以有人請幫我Xamarin.Forms不能隱式地將類型'system.threading.tasks.task>轉換爲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; 
} 

這是顯示

+0

Thankuu費利克斯它真的幫我 –

+0

請接受我的答案,如果它幫你:d –

+1

[xamarin形式不能隱式轉換類型「system.threading.tasks.task <系統的可能的複製.collections.generic list to system.collections.generic List](http://stackoverflow.com/questions/40623973/xamarin-forms-cannot-implicitly-convert-type-system-threading-tasks-tasksystem) –

回答

2

經一個額外的異步方法到您的構造函數的錯誤:

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()結果。

否則返回Task本身。這會導致你的錯誤:

Cannot implicitly convert type 'System.Threading.Tasks.Task' to 'System.Collections.Generic.List< MenuItemModel >.

+0

你真的嘗試過嗎?沒有異步構造函數這樣的事情。 –

+0

@StephaneDelcroix你是對的。沒有想過這個。我修好了它。 –

相關問題