繁体   English   中英

如何将C#接口转换为Java.Lang.Object?

[英]How to cast C# interface to Java.Lang.Object?

目前,我正在Xamarin上开发IEGL10。 我已经实现了ISurfaceHolderCallback并且在SurfaceCreated(ISurfaceHolder holder)我必须调用这样的方法。

 public void SurfaceCreated(ISurfaceHolder holder)
 {
   mEglSurface = mEgl.EglCreateWindowSurface(mEglDisplay, mEglConfig,
            holder, null);
 }

问题是,持有人是C#接口,EglCreateWindowSurface需要Java.Lang.Object。 那我该如何铸造。 如果我直接转换(Java.Lang.Object)holder 它抛出无效的强制转换异常。 请帮助我真的被困在这里的人。

如何将C#接口转换为Java.Lang.Object?

MonoDroid为此目的集成了扩展:

Java.Lang.Object holder_object = holder.JavaCast<Java.Lang.Object>(); 

EGLSurface mEglSurface = mEgl.EglCreateWindowSurface(mEglDisplay, mEglConfig, holder_object, null);

您可以看到该文档:

public static class Extensions
{
    //
    // Summary:
    //     /// Performs an Android runtime-checked type conversion. ///
    //
    // Parameters:
    //   instance:
    //     /// An Android.Runtime.IJavaObject instance to convert /// to a TResult instance.
    //     ///
    //
    // Type parameters:
    //   TResult:
    //     /// The type to convert instance to. /// TResult must implement the /// Android.Runtime.IJavaObject
    //     interface. ///
    //
    // Returns:
    //     /// A TResult representation for /// instance. ///
    //
    // Exceptions:
    //   T:System.ArgumentException:
    //     ///
    //     /// The JNI class for TResult cannot be found. ///
    //     ///
    //     -or-
    //     ///
    //     /// The proxy class for TResult is /// abstract, and the non-abstract Proxy can't
    //     be found. ///
    //     ///
    //
    //   T:System.InvalidCastException:
    //     /// The Anrdroid object instance instance.Handle /// cannot be converted to the
    //     Android type corresponding to /// TResult. ///
    //
    //   T:System.NotSupportedException:
    //     /// An unknown error occurred. ///
    //
    // Remarks:
    //     /// /// This is a hack, but a currently necessary one. /// ///
    //     /// Most of the Android types are staticly generated /// wrappers over a description
    //     of the underlying Android types. This /// intermediate description does not expose
    //     implementation details, /// which sometimes must be relied upon. ///
    //     ///
    //     /// For example, consider the /// Javax.Microedition.Khronos.Egl.EGLContext.EGL
    //     /// property, which returns an instance of the /// Javax.Microedition.Khronos.Egl.IEGL
    //     /// interface. This interface is useless, containing no members to /// invoke
    //     or use. The developer is instead expected to convert this /// instance to an
    //     interface which contains actual operations, such as /// the Javax.Microedition.Khronos.Egl.IEGL10
    //     interface. /// Unfortunately, the MonoDroid-generated wrappers do not know this,
    //     /// nor can they (the EGL10 implementation may be removed in a /// future Android
    //     version). The result is that if developers attempt /// to cast within managed
    //     code, the result will be a /// System.InvalidCastException: ///
    //     /// EGL10 egl10 = (EGL10) EGLContext.EGL; // throws ///
    //     /// The JavaCast() method allows performing such type conversions /// while bypassing
    //     the managed type system and instead relying upon /// the Android runtime system
    //     to perform the type checking. This /// allows: ///
    //     /// EGL10 egl10 = EGLContext.EGL.JavaCast<EGL10>(); // good ///
    public static TResult JavaCast<TResult>(this IJavaObject instance) where TResult : class, IJavaObject;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM