为 Xamarin 创建 Android 绑定时出现问题 [英] Issue while creating Android binding for Xamarin

查看:13
本文介绍了为 Xamarin 创建 Android 绑定时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我们的组织已经创建的库创建一个绑定项目.我添加了 .aar 文件,当我尝试构建时,出现以下两个错误.

I am trying to create a binding project for a library that our organisation is already created. I have added the .aar file and when I try to build I am getting the below two error.

 Error JAVAC0000:  error: OnCompletionListenerImplementor is not abstract and does not override abstract method onCompletion(LockEvent,int,Metadata) in OnCompletionListener

公共类 OnCompletionListenerImplementor (JAVAC0000)

public class OnCompletionListenerImplementor (JAVAC0000)

Error JAVAC0000:  error: SingleStepView_OnSelectionListenerImplementor is not abstract and does not override abstract method onSelected(Metadata,LockEvent) in OnSelectionListener
public class SingleStepView_OnSelectionListenerImplementor
 (JAVAC0000)

API.xaml

它是这样生成的,

<interface abstract="true" deprecated="not deprecated" final="false" name="OnCompletionListener" static="false" visibility="public" jni-signature="Lno/zedoapp/zebra/key_ui/ui/listener/OnCompletionListener;"></interface>
<interface abstract="true" deprecated="not deprecated" final="false" name="SingleStepView.OnSelectionListener" static="true" visibility="public" jni-signature="Lno/zedoapp/zebra/key_ui/ui/view/SingleStepView$OnSelectionListener;"></interface>

我还注意到在对象浏览器中该类没有正确生成.

Also I have noticed in Object browser that the class is not generating properly.

public interface IOnCompletionListener : IJavaObject, IDisposable, IJavaPeerable
{
}

internal sealed class IOnCompletionListenerImplementor : Java.Lang.Object, IOnCompletionListener, IJavaObject, IDisposable, IJavaPeerable
{
    public IOnCompletionListenerImplementor ()
        : base (JNIEnv.StartCreateInstance ("mono/Lno/zedoapp/zebra/key_ui/ui/listener/OnCompletionListenerImplementor", "()V"), JniHandleOwnership.TransferLocalRef);

    internal static bool __IsEmpty (IOnCompletionListenerImplementor value);
}


public interface IOnSelectionListener : IJavaObject, IDisposable, IJavaPeerable
{
}

internal sealed class IOnSelectionListenerImplementor : Java.Lang.Object, IOnSelectionListener, IJavaObject, IDisposable, IJavaPeerable
{
        public IOnSelectionListenerImplementor ()
            : base (JNIEnv.StartCreateInstance ("mono/Lno/zedoapp/zebra/key_ui/ui/view/SingleStepView_OnSelectionListenerImplementor", "()V"), JniHandleOwnership.TransferLocalRef);

        internal static bool __IsEmpty (IOnSelectionListenerImplementor value);
}

谁能帮我解释这个问题并解决我的绑定

Can someone help me to interpret the issue and to solve my binding

推荐答案

在我看来,此链接中的文档有些误导.

The documentation at this link is somewhat misleading in my opinion.

它告诉您需要为生成的 EventArgs 类指定不同的名称,因为绑定生成器不能很好地处理泛型并且无法区分 2 个 Java 接口之间的区别.您可以通过将如下所示的转换规则添加到 Medata.xml 文件来实现:

It tells you that you need to give the generated EventArgs classes different names because the binding generator does not play well with generics and can't tell the different between the 2 Java interfaces. You do that by adding transform rules like the below to the Medata.xml file:

<attr path="/api/package[@name='com.whatever.android']/interface[@name='InterfaceListenerA']/method[@name='onJavaInterfaceMethodName']"
    name="argsType">InterfaceListenerAEventArgs</attr>

<attr path="/api/package[@name='com.whatever.android']/interface[@name='InterfaceListenerB']/method[@name='onJavaInterfaceMethodName']"
    name="argsType">InterfaceListenerBEventArgs</attr>

就我而言,这允许我构建绑定项目.但是当我尝试在 Xamarin Android 项目中引用它并构建它时,我收到了上述错误.

In my case this allowed my binding project to build. But when I tried to reference it in an Xamarin Android project and build it, I get the above error.

文档没有告诉您的是,当您执行此操作时,生成的事件名称将意味着代码不再在生成的 java 代码中实现所需的接口.为此,您需要将 java 接口方法映射到重命名的 C# 事件属性名称.为此,我添加了如下规则:

What the docs don't tell you is that when you do this the generated event name will mean that the code no longer implements the required interface in the generated java code. To make it do that you need to map the java interface method to the renamed C# event property name. To do that I added rules like these:

<attr path="/api/package[@name='com.whatever.android']/interface[@name='InterfaceListenerA']/method[@name='onJavaInterfaceMethodName']"
    name="managedName">InterfaceListenerA</attr>

<attr path="/api/package[@name='com.whatever.android']/interface[@name='InterfaceListenerB']/method[@name='onJavaInterfaceMethodName']"
    name="managedName">InterfaceListenerB</attr>

注意:托管名称不一定与 Java 接口名称相同 - 它是您为每个新 EventArgs 类指定的任何名称,但不带EventArgs".后缀.

Note: The managed name is not necessarily the same as the Java interface name - it's whatever name you've given to each of the new EventArgs classes without the "EventArgs" suffix.

这篇关于为 Xamarin 创建 Android 绑定时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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