如何使用动态回调?

编程入门 行业动态 更新时间:2024-10-24 17:25:09
本文介绍了如何使用动态回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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 显式类型,如果( 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));

  • 更多推荐

    如何使用动态回调?

    本文发布于:2023-08-01 09:16:39,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1267182.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:如何使用   回调   动态

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!