locked
Return arrays to itemsource??? I need help

    Question

  • HI,

    public async System.Threading.Task<string[]> Loading(string id)
    {
    ...
      var srtHtml = await httpClient.GetStringAsync("???");
    ...
     foreach (var node in senseblock)
                        {
    
                            Dictionaries = new Models.EnglishDictionary();
                            var def = node.Descendants("span").FirstOrDefault(x => x.GetAttributeValue("class", "") == "def");
                            var examp = node.Descendants("span").FirstOrDefault(x => x.GetAttributeValue("class", "") == "def-body");
                            //Dictionaries.Symbol = "/Photos/glasses-50.png";
    
                            if (def != null)
                            {
                                Dictionaries.Description = def.InnerText.Trim();
                            }
                            if (examp != null)
                            {
                                Dictionaries.Examp = examp.InnerText.Trim();
                            }
                            arrays.Add(Dictionaries);
    ...
        
    }
     
     private string _description;
            public string Description
            {
                get
                {
                    return _description;
                }
                set
                {
                   _description=value.Replace("\n","");
                }
            }



    "listview.itemsource=loading("1")" in MainPage_Load is show error out. How to get it when object's "async" type. My english is bad so I'm sorry I can't explain clearly by english. 

    Thursday, March 26, 2015 12:36 PM

Answers

  • You must await the Loading method since its return type is Task<string[]> and not string[]:

    async void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                listview.ItemsSource = await Loading("1");
            }

    Please remember to mark all helpful posts as answer to close your threads and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Thursday, March 26, 2015 4:00 PM
  • Since the return type of the method is  System.Threading.Task<Models.MainModels> you must return a single Models.MainModels object:

    return arrays[0];

    If you want to return a List<Models.MainModels> you should change the return type of the method to System.Threading.Task<List<Models.MainModels>>:

    public async System.Threading.Task<List<Models.MainModels>> Loading(string id)
    {
    List<Models.MainModels> arrays = new List<Models.MainModels>();
    ...
    return arrays;
    }

    Please remember to mark all helpful posts as answer and please start a new thread for each new question you may have. Please don't ask several questions in the same thread.

     

    Friday, March 27, 2015 8:39 AM

All replies

  • You must await the Loading method since its return type is Task<string[]> and not string[]:

    async void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                listview.ItemsSource = await Loading("1");
            }

    Please remember to mark all helpful posts as answer to close your threads and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Thursday, March 26, 2015 4:00 PM
  • In 

    public async System.Threading.Task<Models.MainModels> Loading(string id)

    {

    List<Models.MainModels> arrays = new List<Models.MainModels>();

    await....

    ...

    arrays.Add(t);

    ...

    what to return???

    }

    Return arrays it show error out.


    Thursday, March 26, 2015 10:47 PM
  • Since the return type of the method is  System.Threading.Task<Models.MainModels> you must return a single Models.MainModels object:

    return arrays[0];

    If you want to return a List<Models.MainModels> you should change the return type of the method to System.Threading.Task<List<Models.MainModels>>:

    public async System.Threading.Task<List<Models.MainModels>> Loading(string id)
    {
    List<Models.MainModels> arrays = new List<Models.MainModels>();
    ...
    return arrays;
    }

    Please remember to mark all helpful posts as answer and please start a new thread for each new question you may have. Please don't ask several questions in the same thread.

     

    Friday, March 27, 2015 8:39 AM
  • I got the error:system.threading.tasks can not be used with argument at 
    System.Threading.Task<List<Models.MainModels>>

    Tuesday, March 31, 2015 12:37 AM