如何使用动态回调? [英] How to do Callbacks with Dynamic?

查看:100
本文介绍了如何使用动态回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直收到此错误,不确定如何纠正

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 and email explicit types, which (if userService isn't dynamic) 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 from dynamic:

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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆