ovesco
ovesco

Reputation: 633

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 !

Upvotes: 2

Views: 5194

Answers (2)

Stephen Cleary
Stephen Cleary

Reputation: 457402

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.

Upvotes: 1

user176134
user176134

Reputation:

I'm not absolutely sure what you are trying to do but what's wrong with simply:

string test = await GetRandomRelations ();

Upvotes: 0

Related Questions