将Android代码片段从Java转换为C(内联类扩展)
我正在尝试将Android库从Java转换为C#(Xamarin) 看看类中私有字段的定义:将Android代码片段从Java转换为C(内联类扩展),java,c#,android,xamarin,Java,C#,Android,Xamarin,我正在尝试将Android库从Java转换为C#(Xamarin) 看看类中私有字段的定义: private SimpleOnGestureListener longClickListener = new GestureDetector.SimpleOnGestureListener() { public void onLongPress(MotionEvent e) { hasPerformedLongPress = childView.performLongClick
private SimpleOnGestureListener longClickListener = new GestureDetector.SimpleOnGestureListener() {
public void onLongPress(MotionEvent e) {
hasPerformedLongPress = childView.performLongClick();
if (hasPerformedLongPress) {
if (rippleHover) {
startRipple(null);
}
cancelPressedEvent();
}
}
@Override
public boolean onDown(MotionEvent e) {
hasPerformedLongPress = false;
return super.onDown(e);
}
};
据我所知,在C#中无法创建类的“内联扩展”
您将如何转换这段代码?请记住,“hasPerformedLongPress”和“rippleHover”是类的私有字段。您可以创建一个实现接口的新私有类-您可以通过内部类的“outerInstance”字段访问外部类的字段。但是,我能做到这一点的唯一方法是将“longClickListener”字段更改为局部变量,以便能够将“this”传递给新的私有类构造函数-因为不能从C#字段定义中使用“this”:
internal class test
{
private bool rippleHover;
private bool hasPerformedLongPress;
internal virtual void method()
{
SimpleOnGestureListener longClickListener = new SimpleOnGestureListenerAnonymousInnerClassHelper(this);
}
private class SimpleOnGestureListenerAnonymousInnerClassHelper : GestureDetector.SimpleOnGestureListener
{
private readonly test outerInstance;
public SimpleOnGestureListenerAnonymousInnerClassHelper(test outerInstance)
{
this.outerInstance = outerInstance;
}
public virtual void onLongPress(MotionEvent e)
{
outerInstance.hasPerformedLongPress = childView.performLongClick();
if (outerInstance.hasPerformedLongPress)
{
if (outerInstance.rippleHover)
{
startRipple(null);
}
cancelPressedEvent();
}
}
public override bool onDown(MotionEvent e)
{
outerInstance.hasPerformedLongPress = false;
return base.onDown(e);
}
}
}
您可以创建一个实现接口的新私有类-您可以通过内部类的“outerInstance”字段访问外部类的字段。但是,我能做到这一点的唯一方法是将“longClickListener”字段更改为局部变量,以便能够将“this”传递给新的私有类构造函数-因为不能从C#字段定义中使用“this”:
internal class test
{
private bool rippleHover;
private bool hasPerformedLongPress;
internal virtual void method()
{
SimpleOnGestureListener longClickListener = new SimpleOnGestureListenerAnonymousInnerClassHelper(this);
}
private class SimpleOnGestureListenerAnonymousInnerClassHelper : GestureDetector.SimpleOnGestureListener
{
private readonly test outerInstance;
public SimpleOnGestureListenerAnonymousInnerClassHelper(test outerInstance)
{
this.outerInstance = outerInstance;
}
public virtual void onLongPress(MotionEvent e)
{
outerInstance.hasPerformedLongPress = childView.performLongClick();
if (outerInstance.hasPerformedLongPress)
{
if (outerInstance.rippleHover)
{
startRipple(null);
}
cancelPressedEvent();
}
}
public override bool onDown(MotionEvent e)
{
outerInstance.hasPerformedLongPress = false;
return base.onDown(e);
}
}
}
我认为不可能访问外部类的私有字段?我更新了我的答案-但是它可能不适合您,因为我将字段更改为“方法”中的局部变量。你应该能够通过这个来获得你想要的(更改回一个字段,但在一个方法中初始化它?)。我认为不可能访问外部类的私有字段?我更新了我的答案-但是它可能不适合你,因为我将字段更改为“方法”中的局部变量。您应该能够利用它来获得您想要的(更改回字段,但在方法中初始化它?)。