【发布时间】:2018-04-29 16:41:03
【问题描述】:
当我运行这些代码时,我应该会看到一行包含一些值(包括价格)已添加到我的列表中。 getpriceAsync 方法是我创建的一种使用JSON 从特定网站获取数据的方法,需要等待,它需要一个链接作为参数。我试图将它从foreach 循环中取出并且它起作用了,它仅在它位于foreach 循环内时才会卡住,尽管它不会引发错误并且它没有向我显示我的列表中应该包含string price 的行.我无法将 await 表达式放在循环之外,因为它需要项目的名称作为参数。
请问我的问题有什么解决办法吗?
谢谢..
protected async override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.layout);
string price = "";
var db = new SQLiteConnection(Class1.dpPath);
var table = db.Table<Table>();
foreach(var item in table)
{
if(item.value != "0" && item.quantity != "0")
{
price = await GetpriceAsync(item.name); // here is the line of the await keyword
Class1.tableItems.Add(new TableItem(item.quantity, price, "1000"));
}
}
}
【问题讨论】:
-
如果
if statement在foreach循环之外是否有效? -
当然不是,因为 if 语句的条件与 foreach 循环遍历的表中的项目有关。另外,我试图删除 if 语句并将我的 await 表达式保留在 foreach 中,但也没有用。
-
您使用错误的方法从数据库中选择数据。
-
尝试选择数据,如下所示stackoverflow.com/questions/26646399/…
标签: c# android visual-studio xamarin foreach