我在写一个简单的Silverlight应用程序和WCF服务。 我想创建一个返回值的同步方法。 该方法本身从WCF服务调用异步方法。在我调用异步方法之后,我想获取它的值,并返回给发件人。 我听说Rx可以解决这类问题。Silverlight:使用Rx从同步方法返回值
这是我的代码:
private void btnCreate_Click(object sender, RoutedEventArgs e)
{
string myResult = getMyBook(txtBookName.Text);
MessageBox.Show("Result\n" + myResult);
// myResult will be use for another purpose here..
}
// I want this method can be called anywhere, as long as the caller still in the same namespace.
public string getMyBook(string bookName)
{
Servo.ServoClient svc = new ServoClient();
string returnValue = "";
var o = Observable.FromEventPattern<GetBookCompletedEventArgs>(svc, "GetBookCompleted");
o.Subscribe(
b => returnValue = b.EventArgs.Result
);
svc.GetBookAsync(bookName);
return returnValue;
}
当我点击的BtnCreate,myResult变量仍然是空的。我的代码有问题吗?或者,也许我只是不明白Rx概念?我是Rx的新手。
我的目标是:我需要的结果从异步方法(myResult变量),然后在后面的代码中使用。