如何在 xamarin 上实现 Handle、JniIdentityHashCode、PeerReference、JniPeerMembers、JniManagedPeerState

how to implement Handle,JniIdentityHashCode,PeerReference,JniPeerMembers,JniManagedPeerState on xamarin

您好,我该如何实现以下功能?在 xamarin 上

  public IntPtr Handle => throw new NotImplementedException();

        public int JniIdentityHashCode => throw new NotImplementedException();

        public JniObjectReference PeerReference => throw new NotImplementedException();

        public JniPeerMembers JniPeerMembers => throw new NotImplementedException();

        public JniManagedPeerStates JniManagedPeerState => throw new NotImplementedException();

您在 Android 项目中的 C# 对象应继承自 Java.Lang.Object

public class YourCSharpObject : Java.Lang.Object
{
}

在xamarin中,如果我们实现接口,我们需要继承class Java.Lang.Object以及接口本身,就像Trevor Balcom提到的。

假设你的界面是ITestClientListener,我们可以这样做:

public class TestClientListener : Java.Lang.Object, ITestClientListener
{
    public JniManagedPeerStates JniManagedPeerState()
    {
        throw new NotImplementedException();
    }

    IntPtr ITestClientListener.Handle()
    {
        throw new NotImplementedException();
    }

    int ITestClientListener.JniIdentityHashCode()
    {
        throw new NotImplementedException();
    }

    JniPeerMembers ITestClientListener.JniPeerMembers()
    {
        throw new NotImplementedException();
    }

    JniObjectReference ITestClientListener.PeerReference()
    {
        throw new NotImplementedException();
    }
}

我们可以像这样使用它的实例:

//create a new object
TestClientListener listener = new TestClientListener();

// use it as you need

注:

如果需要,您需要在class TestClientListener .cs中的上述方法中添加必要的代码。