简体   繁体   English

Xamarin中用于视图的SetListener

[英]SetListener for View in Xamarin

I'm trying to port some Java code in to C#. 我正在尝试将一些Java代码移植到C#中。 where CW is a class which extends a view. 其中CW是扩展视图的类。 OnSelectedListener is a interface with Cselected as method which takes an int argument. OnSelectedListener是带有Cselected作为方法的接口,该接口带有int参数。

setListener is a method within the class. setListener是类中的方法。 the problem is with instantiate a interface like in Java. 问题在于实例化Java中的接口。

private View selectedView = new View( context );

        CW.setListener( new OnSelectedListener() {
    @Override
    public void cSelected(Integer color) {
    selectedColor = color;
    selectedView.setBackgroundColor( color );
    }
    });

Another Implementation in same method 同样方法的另一种实现

VS.setListener( new OnSelectedListener() {
public void cSelected(Integer color) {
VS.setColor( color, true );
}
} );

Can anyone please help me port the above code to C#? 谁能帮我将上面的代码移植到C#中? Any help is appreciated. 任何帮助表示赞赏。 I'm using Xamarin to develop Android apps. 我正在使用Xamarin开发Android应用程序。

EDIT: 编辑:

Here is the full CW class 这是完整的CW课程

 public class HSVColorWheel : View
        {
            private const float SCALE = 2f;
            private const float FADE_OUT_FRACTION = 0.03f;
            private const int POINTER_LINE_WIDTH_DP = 2;
            private const int POINTER_LENGTH_DP = 10;

            private Context _context;

            public HSVColorWheel(Context context, IAttributeSet attrs, int defStyle)
                : base(context, attrs, defStyle)
            {
                this._context = context;
                Init();
            }

            public HSVColorWheel(Context context, IAttributeSet attrs) : base(context, attrs)
            {

                this._context = context;
                Init();
            }

            public HSVColorWheel(Context context) : base(context)
            {
                this._context = context;
                Init();
            }

            private int scale;
            private int pointerLength;
            private int innerPadding;
            private Paint pointerPaint = new Paint();

            private void Init()
            {
                float density = _context.Resources.DisplayMetrics.Density;
                scale = (int) (density*SCALE);
                pointerLength = (int) (density*POINTER_LENGTH_DP);
                pointerPaint.StrokeWidth = (int) (density*POINTER_LINE_WIDTH_DP);
                innerPadding = pointerLength/2;
            }

            public void setListener(OnSelectedListener listener)
            {
                _listener = listener;
            }

            private float[] colorHsv = {0f, 0f, 1f};

            public void setColor(Color color)
            {

                Color.ColorToHSV(color, colorHsv);
                Invalidate();
            }
}

Interface: 接口:

public interface OnSelectedListener {

void cSelected( Integer color );
}

As mentioned in the comments, since C# has language-level support for events , it provides a much cleaner approach than java's "even listener" approach. 如评论中所述,由于C# 对事件具有语言级别的支持 ,因此它提供了比Java的“甚至侦听器”方法更简洁的方法。

Therefore, all listener-based java code should be converted into proper events in C#. 因此,所有基于侦听器的Java代码都应在C#中转换为适当的事件。

In this case, you're seemingly raising an event that has an int parameter. 在这种情况下,您似乎正在引发一个具有int参数的事件。 This is declared in C# like so: 这是在C#中声明的,如下所示:

//In the CW class:
public event EventHandler<int> SelectionChanged;

and then raised via an "event invocator", like so: 然后通过“事件发起者”提出,例如:

//In the CW class:
public void OnSelectionChanged()
{
    var handler = SelectionChanged;
    if (handler != null)
         handler(this, //[ some int value here ]);
}

from the "consumer", or "listener" side, you simply handle the event: 从“消费者”或“听众”方面,您只需处理事件:

//In an Activity
var CW = new CW(this);

CW.SelectionChanged += CW_SelectionChanged;

where CW_SelectionChanged can either be a an anonymous method, an actual named method, or even a lambda expression: 其中CW_SelectionChanged可以是匿名方法,实际的命名方法甚至lambda表达式:

CW.SelectionChanged += (sender, intValue) => //[here you do something with intValue]

// -- OR --

CW.SelectionChanged += this.CW_SelectionChanged;

// then
private void CW_SelectionChanged(object sender, int intValue)
{
   //[here you do something with intValue]
}

This way, you don't need to declare additional, unneeded 1-method interfaces. 这样,您无需声明其他不需要的1方法接口。

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

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