简体   繁体   English

xamarin.forms中页面加载时的异步

[英]async on page load in xamarin.forms

I'm currently developping a small application based on a Master Detail template. 我目前正在基于Master Detail模板开发一个小型应用程序。 One of my Pages requires some data to be loaded immediatly, and I dont know how to do this. 我的Pages需要立即加载一些数据,我不知道该怎么做。 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... 我目前正试图获取json响应,但我什至无法做到这一点...我的主要问题是我无法将lambda表达式转换为字符串...

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. UI必须是响应式的。 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. 相反,您必须以不同的方式满足这两个核心要求(“ UI必须具有响应能力”和“异步检索数据”)。 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 ();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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