1

我有一个方法需要返回布尔值,在我的方法中我需要进行异步调用来决定返回 true 还是 false。我试图将 return 语句放在 lambda 表达式中,但它会抛出一个return type is 'void'错误

bool method()
{
    domaincontext.Load(domaincontext.GetXXX(),
    loadOperation =>
    {
    value = ???
    }, null);

    return value;
}
4

1 回答 1

1

你不能这样编码。Silverlight 将不允许您查询 Web 服务并冻结 UI,直到 Web 服务返回。Silverlight 的异步模型更像 javascript,您可以在其中进行调用,当结果返回时,您可以决定要使用它做什么。

一种方法是将调用者的代码更改为如下所示:

this.method(result => {
  if (result) {
     // Do something
  }
});

void method(Action<bool> continueWith)
{
    domaincontext.Load(domaincontext.GetXXX(),
    loadOperation =>
    {
        value = ???;
        continueWith(value);
    }, null);

    return value;
}
于 2012-09-28T05:11:00.550 回答