c# - 如何在 Xamarin.Android 中的按钮上调用异步非 lambda 函数
问题描述
例如,我已经编写了一些异步函数,并希望在单击按钮时调用它。像这样:
static async Task<string> ParseSth(string URL) { ... }
当我单击此按钮时,我想调用它:
FindViewById<Button>(Resource.Id.ButtonParse).Click += ...
在 google 或 youtube 中,我只找到了关于 lamda 表达式的材料。那么,如何做到这一点呢?
解决方案
您可以使您的点击处理程序异步,然后从那里调用 ParseSth 并等待。只要知道您的方法抛出的任何异常都不会被捕获,因为您将异步添加到 void 方法。
private async void button_Click(object sender, EventArgs e)
{
await ParseSth(myTextBox.Text); // Any exception thrown here will be lost
}
一些 MVVM 框架的另一个解决方案可能是将异步命令绑定到您的按钮单击。
推荐阅读
- c - 指数动态内存分配
- model - 我可以从搅拌机的控制台导出模型吗?
- c++-cli - 如何在换行符处拆分字符串?
- python - 对我的数据框使用 agg(),没有结果
- regex - 如果在 YML 文件中使用正则表达式为空,则删除第一行
- angular - Leaflet map draggable - 如何在拖动事件中调用函数或更改类变量
- python-3.x - 如何从列表中获取字符串并将其与另一个字符串进行比较?
- html - 我可以在我的 Github 存储库的 README.md 中嵌入 Plotly 生成的 html 文件吗?
- java - SMALL_PRIME_PRODUCT 在 BigInteger 中的意义
- nginx - 在反向代理服务器 + Python HTTPS 服务器中,谁应该处理 HTTPS 连接的 SSL 证书?