2017-11-10 2 views
0

현재 저는 Xamarin에서 IEGL10을 연구하고 있습니다. 나는 ISurfaceHolderCallback을 구현했으며 SurfaceCreated(ISurfaceHolder holder)에 나는 이와 같은 메소드를 호출해야한다.C# 인터페이스를 Java.Lang.Object로 캐스트하는 방법은 무엇입니까?

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

문제는 홀더가 C# 인터페이스이고 EglCreateWindowSurface가 Java.Lang.Object를 필요로한다는 것입니다. 어떻게 캐스팅을 할 수 있습니까? 제가 직접 (Java.Lang.Object)holder과 같은 홀더를 던지면. 잘못된 캐스트 예외가 발생합니다. 나는 정말로 여기에 갇혀있는 사람들을 도와주세요.

+1

이 질문에 대답하기 위해 나는 자 마린에 충분히 익숙하지 않다 :

Java.Lang.Object holder_object = holder.JavaCast<Java.Lang.Object>(); EGLSurface mEglSurface = mEgl.EglCreateWindowSurface(mEglDisplay, mEglConfig, holder_object, null); 

당신은 문서를 볼 수 있었다. 그러나, 당신이 참조하고있는'Java.Lang.Object' 타입이 C# 타입이고, 여기서'ISurfaceHolder'를 구현 한 클래스의 타입 계층 구조를 변경할 수 없다면, 당신은 'holder' 오브젝트는'Java.Lang.Object'를 상속받은 다른 타입으로'ISurfaceHolder'를 구현하고, 모든 인터페이스 멤버를 원래의'holder' 오브젝트에 위임합니다. –

+0

SurfaceCreated (ISurfaceHolder holder)는 ISurfaceHolderCallback의 재정의 된 메서드입니다. 그래서 홀더 객체를 감쌀 수 없습니다. –

+0

_ "홀더 객체를 감쌀 수 없습니다."_ - 나에게 단조로운 것처럼 보입니다. 첫 번째 문장은 두 번째 문장과 무슨 상관이 있습니까? 귀하의 게시물은 _you_가'SurfaceCreated()'메소드를 작성했으며'EglCreateWindowSurface()'메소드를 호출하고 있음을 의미합니다. 따라서,'holder' 객체가'EglCreateWindowSurface()'메소드에 전달되기 전에 접근 할 수 있고 거기에 그것을 감쌀 수 있습니다. 요점 : 어떻게 든, 당신은'Java.Lang.Object' 객체를 만들어 내야 만합니다. 만약'holder '가 그렇게 할 수 없다면, 당신은 다른 것을 넘겨야 만합니다. –

답변

1

Java.Lang.Object에 C# 인터페이스를 캐스팅하는 방법은 무엇입니까?

MonoDroid

이 목적을 위해 확장을 통합했습니다

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; 
}