如何避免将我的整个代码变为静态

时间:2017-02-22 21:03:01

标签: c# xamarin

在此代码中继承了一个名为ISurfaceTextureListener的接口

class Camera
{
    TextureView mTextureView;
    Context _context;
    public Camera (Context context, TextureView textureView)
    {
        _context = context;
        mTextureView = textureView;
        mTextureView.SurfaceTextureListener = new TextureViewListener();
    }
    private class TextureViewListener : Java.Lang.Object, TextureView.ISurfaceTextureListener
    {
        public void OnSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
        {
            OpenCamera(width, height); //Error Occurring Here 
        }
    }
    public void OpenCamera(int width, int height)
    {
        //Codes 
    }
}
  

错误:访问非静态字段需要对象引用,   方法等。

我不想让OpenCamera()静态,因为我必须把我的整个代码变成静态方法,那么有没有办法避免它?

注意:我只是继承了界面,因为我无法覆盖对象监听器" OnSurfaceTextureAvailable"方法,我找到的唯一方法是为对象的侦听器分配一个继承的类,它工作得很好。

1 个答案:

答案 0 :(得分:3)

错误与您的想法完全相反。这不是暗示你应该OpenCamera()静态的;这是一个提示,你试图访问它,就像它是静态的,而实际上它不是。

您需要someObjectIhaventToldYouAnythingAbout.OpenCamera(width, height);

修改

因此,在您的评论和编辑后,您基本上告诉我们someObjectIhaventToldYouAnythingAbout,您似乎应该这样做:

    mTextureView.SurfaceTextureListener = new TextureViewListener( this );
}
private class TextureViewListener : Java.Lang.Object, TextureView.ISurfaceTextureListener
{
     readonly Camera camera;

     TextureViewListener( Camera camera )
     {
         this.camera = camera;
     }

    public void OnSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
    {
        camera.OpenCamera(width, height); //Error Occurring Here 
    }
}
public void OpenCamera(int width, int height)
{
    //Codes 
}