【发布时间】:2021-01-22 17:10:20
【问题描述】:
我一直在尝试在 Xamarin 项目中使用我的 Android 库(用 Kotlin 编写),但我一直坚持将 Lambda 函数传递给 C# 生成的 Kotlin 代码
我正在尝试做这样的事情
client.DoSomething((response) => {}, (error) => {});
但是我收到了这个错误
CS1660: Cannot convert lambda expression to type 'IFunction1' because it is not a delegate type
这是为此特定函数为我的库生成的 C# 代码
using Android.Runtime;
using Java.Interop;
using Java.Lang;
using Kotlin.Jvm.Functions;
using System;
[Register ("doSomething", "(Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function1;)V", "")]
public unsafe void DoSomething (IFunction1 onSuccess, IFunction1 onFailure);
这样做的正确方法是什么?
【问题讨论】:
-
可以将 lambda 表达式转换为委托类型。但在您的情况下,lambda 表达式将转换为不支持的 'IFunction1' 类型。尝试传递 'IFunction1' 类型的参数。
标签: c# android kotlin xamarin xamarin.android