为Xamarin Android控件创建自定义对象包装

时间:2019-09-02 19:15:20

标签: c# xamarin

我对Xamarin和C#还是陌生的,我正在寻找一种使我的目标代码可重用的方法,而不必为大量项目复制/粘贴类似的代码块。

目前,我正在使用控件上的RatingBar对象。我有大约20个行为几乎相同。不幸的是,由于对C#或Xamarin都不了解,因此我无法进行Google搜索,从而无法获得有用的结果。

下面是布局中两个类似项目的示例:

[
2, 79.98, 167, '1',
2, 19.98, 167, '2',
2, 79.98, 168, '1',
2, 79.98, 169, '3',
2, 79.98, 170, '4'
]

以及“活动”中的关联代码:

'''XML
     <TextView
        android:text="Death"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lbl_death" />
    <RatingBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/arcanadeath"
        android:numStars="5"
        android:rating="1"
        android:stepSize="1" />
    <TextView
        android:text="Fate"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lbl_fate" />
    <RatingBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/arcanafate"
        android:numStars="5"
        android:rating="1"
        android:stepSize="1" />

'''

如果有人可以告诉我如何将其包装到类或某种可重用的对象中,或者将我定向到我应该寻找的内容以了解如何执行此操作,那么我将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:1)

您可以创建一个简单的自定义组件,如下所示:

创建 RateLayout.cs

class RateLayout : LinearLayout
{
    private RateChangeListener listener;
    protected RateLayout(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
    {

    }

    public RateLayout(Context context) : base(context)
    {

        init(context);
    }

    public RateLayout(Context context, IAttributeSet attrs) : base(context, attrs)
    {
        init(context);
    }

    public RateLayout(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
    {
        init(context);
    }
    public void SetRateChangeListener(RateChangeListener listener)
    {
        this.listener = listener;
    }
    private void init(Context context)
    {
        LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent);
        View view = LayoutInflater.From(context).Inflate(Resource.Layout.rate_layout,null);
        view.LayoutParameters = parms;
        RatingBar death = view.FindViewById<RatingBar>(Resource.Id.arcanadeath);

        death.RatingBarChange += (sender, e) =>
        {
            listener.RateDeathChange((int)death.Rating);
        };

        RatingBar fate = view.FindViewById<RatingBar>(Resource.Id.arcanafate);

        fate.RatingBarChange += (sender, e) =>
        {
            listener.RateDeathChange((int)fate.Rating);
        };
        AddView(view);
    }

}

创建一个界面 RateChangeListener.cs

interface RateChangeListener
{
    void RateDeathChange(int rate);
    void RateFateChange(int rate);
}

然后,当您添加到活动的布局axml中时:

 ...
 <App2.RateLayout 
    android:id="@+id/ratelayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

 />
 ...

活动代码中:

public class YourActivity: Activity,RateChangeListener
{
   protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_layout);
        ...
        RateLayout rateLayout = FindViewById<RateLayout>(Resource.Id.ratelayout);
        rateLayout.SetRateChangeListener(this);
    }

   public void RateDeathChange(int rate)
    {
        Toast.MakeText(this, rate + "  star", ToastLength.Short).Show();
    }

    public void RateFateChange(int rate)
    {
        Toast.MakeText(this, rate + "  star", ToastLength.Short).Show();
    }
}
相关问题