Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

how to fire Task() with async functions? how can I check they are running or not?

$
0
0

hi all,

I'm having problems trying to launch an async method in a task object. Essentially the problem is that either it doesn't start at all or that it starts in synchronous and I can't get the json information, or calculate the device position in latiude and longitude. Here is my code:

For the httpClient:

using System;
using System.Net.Http;
using Newtonsoft.Json;
using System.Threading.Tasks;

namespace MetlivingApp
{
    public class MLWebService
    {
        public MLWebService ()
        {

        }

        public async Task<Propiedad[]> GetAllPropertiesAsync () {

            var client = new HttpClient ();

            client.BaseAddress = new Uri("http://website.net/api/");

            var response = await client.GetAsync("get.php"); //TODO agregar los valores que van en el get.php para los diferentes queries

            var propiedadesJson = await response.Content.ReadAsStringAsync();

            var rootobject = JsonConvert.DeserializeObject<RootObject>(propiedadesJson);

            return rootobject.propiedades;

        }
    }
}

Now here is where I try to run this inside a ContentPage, and it doesn't work:

var webservice = new MLWebService ();
Task<Propiedad[]> t = webservice.GetAllPropertiesAsync ();
t.Start ();
t.Wait ();
armaPantalla(t.Result);  //method that builds the contentpage with a ListView

. . .

//part of armaPantalla(t.Result)
PropertyList = new ListView ();
var cell = new DataTemplate (typeof(ImageCell));
cell.SetBinding (ImageCell.TextProperty, "titulo_es");
cell.SetBinding (ImageCell.DetailProperty, "descripcion");
cell.SetBinding (ImageCell.ImageSourceProperty, "ruta_imagen_portada");
PropertyList.ItemTemplate = cell;
PropertyList.ItemsSource = t;

I also try to use it with the X.Labs location services to get the coordinates of the device, but again the async method never starts. How can I print in the application output? I've tried to make some debug code but don't know how to either.

Any help would be greatly appreciated, I don't know what else to try.


Viewing all articles
Browse latest Browse all 58056

Trending Articles