如何使用动态回调? [英] How to do Callbacks with Dynamic?
问题描述
我一直收到此错误,不确定如何纠正
I keep getting this error and not sure how to correct it
错误1无法将回调用作动态参数
分派的操作,因为它是一个方法组。您是否打算
调用该方法?
Error 1 Cannot use 'Callback' as an argument to a dynamically dispatched operation because it is a method group. Did you intend to invoke the method?
//...
if (e.Status == LiveConnectSessionStatus.Connected)
{
client = new LiveConnectClient(e.Session);
LiveOperationResult operationResult = await client.GetAsync("me");
try
{
dynamic meResult = operationResult.Result;
var openId = meResult.id;
var email = meResult.emails.preferred;
//MessageBox.Show(openId);
//MessageBox.Show(email);
userService.SignIn(openId, email, Callback);
}
catch (LiveConnectException exception)
{
MessageBox.Show("Error calling API: " + exception.Message);
}
}
}
private void Callback(ErrorModel error)
{
if (error != null)
{
MessageBox.Show(error.FriendlyErrorMsg, error.Caption, MessageBoxButton.OK);
}
else
{
}
}
public void SignIn(string id, string email, Action<ErrorModel> callBack)
{
}
推荐答案
问题在于此调用是动态的:
The problem is that this call is dynamic:
userService.SignIn(openId, email, Callback);
必须是,因为 openId
和电子邮件
推断为动态
类型:
It has to be, because openId
and email
are inferred to be of type dynamic
:
var openId = meResult.id;
var email = meResult.emails.preferred;
在动态调用中不能使用这样的方法组转换-这只是其中之一限制使用 dynamic
。
You can't use a method group conversion like this in a dynamic call - it's just one of the restrictions of using dynamic
.
因此,选项:
-
给出
openId
和email
显式类型,如果(< c $ c> userService 不是动态的dynamic
有一个隐式转换:
Give
openId
andemail
explicit types, which (ifuserService
isn'tdynamic
) will make the call non-dynamic, at which the method group conversion will work. This just means specifying the types explicitly, as there's an implicit conversion available fromdynamic
:
string openId = meResult.id;
string email = meResult.emails.preferred;
userService.SignIn(openId, email, Callback);
从<$ c创建一个特定的委托类型实例$ c>回调方法,如果要保持 SignIn
调用动态:
Create a specific delegate type instance from the Callback
method, if you want to keep the SignIn
call dynamic:
var openId = meResult.id;
var email = meResult.emails.preferred;
// Or use whichever delegate type is actually appropriate for SignIn
userService.SignIn(openId, email, new Action<ErrorModel>(Callback));
这篇关于如何使用动态回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!