简体   繁体   中英

async on page load in xamarin.forms

I'm currently developping a small application based on a Master Detail template. One of my Pages requires some data to be loaded immediatly, and I dont know how to do this. In every example, data is loaded once user press a button.

Here is my current code :

string test = async (sender, e) => {

    Task<string> json = GetRandomRelations ();
    return await json;
};

And my method

public async Task<string> GetRandomRelations () {

        var client              = new System.Net.Http.HttpClient ();
        client.BaseAddress      = new Uri("http://127.0.0.1/loltools/web/app_dev.php/api/relation/");
        string response         = await client.GetStringAsync("random/20");

        return response;
    }

I'm currently just trying to get the json response, but I cannot even manage to do that... My main problem is that I cannot convert the lambda expression to string...

Thanks for your help !

One of my Pages requires some data to be loaded immediatly, and I dont know how to do this.

Think about this for a bit. What you're really asking is how to reconcile two opposing requirements:

  1. The page must show some data immediately. The UI must be responsive. The data must be available synchronously to display.
  2. The data is retrieved asynchronously. It is not available immediately. It will take some (unknown) amount of time to even get the data to display.

So, obviously, there's no direct solution. Instead, you have to satisfy the both of the core requirements ("The UI must be responsive" and "The data is retrieved asynchronously") in a different way. One common approach is to (immediately and synchronously) display a "Loading..." view of the data - a spinner or whatnot. Then, update the display when the data arrives.

我不确定您要做什么,但简单地说出了什么问题:

string test = await GetRandomRelations ();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM