【发布时间】:2014-10-28 13:35:02
【问题描述】:
我正在尝试创建一个库,它只有一个公共函数,它返回一个枚举。由于 android 平台,根据我的要求为此提供动力的私有功能必须是异步的。我想做的是让唯一的公共函数作为非异步函数使用。我知道这可能违反直觉,但我有我的理由。就函数的使用者而言,他们以前总是必须以同步的方式运行代码,而现在这个要求并没有改变,因为它被包装在一个更高效的库中。我不想让他们做等待方法。他们不必知道,这就是幕后发生的事情。
这是我到目前为止所拥有的,但我正在努力使用公共方法:
public enum DialogResult
{
Button1 = 1,
Button2 = 2,
Button3 = 3,
}
class MsgBox
{
public DialogResult Show(String Title, String Caption, Context context)
{
}
private async Task<DialogResult> ShowAsync(String Title, String Caption, Context context)
{
return await BuildAsync(Title, Caption, context);
}
private Task<DialogResult> BuildAsync(String Title, String Caption,Context context)
{
var r = new TaskCompletionSource<DialogResult>();
new AlertDialog.Builder(context)
.SetPositiveButton("Yes", (sender, args) =>
{
// User pressed yes
r.SetResult(DialogResult.Button1);
})
.SetNegativeButton("No", (sender, args) =>
{
// Do nothing
r.SetResult(DialogResult.Button2);
})
.SetMessage(Caption)
.SetTitle(Title)
.Show();
return r.Task;
}
}
总结;我需要这个公共函数在它从 ShowAsync 返回后只返回我的普通 DialogResult,而不需要用 await 调用它自己。
【问题讨论】:
-
喜欢
ShowAsync(...).Result? -
感谢 DavidG,但我试过了,它使 UI 死锁。
-
ConfigureAwait怎么样? -
它需要是异步的。这只是使用这样的 UI 的本质所固有的。您将无法使该方法同步;任何这样做的尝试都只会阻塞 UI 线程。
-
不要做“同步异步”。选择,要么一直异步,要么一直同步。如果您的消费者必须是同步的,请不要使用异步。
标签: c# android asynchronous xamarin async-await