我想通過使用Xamarin.Android擴展Android核心ImageView類來創建自定義ImageView類。下面是Java中的一段代碼和C#中不完整的實現。我需要最後2種方法的幫助。將Android Custom ImageView轉換爲Xamarin.Android
ANDROID Java代碼
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ImageView;
public class IconViewView extends ImageView {
private ColorStateList tint;
public IconView(Context context) {
super(context);
}
public IconView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}
public IconView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs, defStyle);
}
private void init(Context context, AttributeSet attrs, int defStyle) {
TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.IconView, defStyle, 0);
tint = a.getColorStateList(R.styleable.IconView_iconTint);
a.recycle();
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (tint != null && tint.isStateful()) {
updateTintColor();
}
}
public void setColorFilter(ColorStateList tint) {
this.tint = tint;
super.setColorFilter(tint.getColorForState(getDrawableState(), 0));
}
private void updateTintColor() {
int color = tint.getColorForState(getDrawableState(), 0);
setColorFilter(color);
}
}
ANDROID XAMARIN C#代碼
using Android.Content;
using Android.Content.Res;
using Android.Graphics;
using Android.Support.V4.Content;
using Android.Support.V4.Graphics.Drawable;
using Android.Util;
using Android.Widget;
namespace Example.Droid.App.Views
{
public class IconView : ImageView
{
private ColorStateList tint;
private Context context;
public IconView(Context context) :base(context)
{
Initialize(context, null, 0);
}
public IconView(Context context, IAttributeSet attrs) :
base(context, attrs)
{
Initialize(context, attrs, 0);
}
public IconView(Context context, IAttributeSet attrs, int defStyle) :
base(context, attrs, defStyle)
{
Initialize(context, attrs, defStyle);
}
void Initialize(Context mContext, IAttributeSet attrs, int defStyle)
{
context = mContext;
TypedArray a = context.ObtainStyledAttributes(attrs, Resource.Styleable.IconView, defStyle, 0);
tint = a.GetColorStateList(Resource.Styleable.IconView_iconTint);
a.Recycle();
}
protected override void DrawableStateChanged()
{
base.DrawableStateChanged();
if (tint != null && tint.IsStateful)
UpdateTintColor();
}
private void UpdateTintColor() {
/* I NEED HELP HERE */
}
public void SetColorFilter(ColorStateList tint) {
/* I NEED HELP HERE */
}
}
}
我需要一些幫助與Xamarin C#
private void UpdateTintColor() {
/* I NEED HELP HERE */
}
public void SetColorFilter(ColorStateList tint) {
/* I NEED HELP HERE */
}
您需要將「override」關鍵字添加到任何重寫的方法,並且您可以通常使用Java使用'super'的C#'base'關鍵字。 – Jason
謝謝傑森。這裏有什麼。'私人無效UpdateTintColor(){ /*我需要幫助HERE * /} 公共 無效SetColorFilter(ColorStateList色調){ /*我需要幫助HERE */ }' – Bismarck