0

I'm new to both Xamarin and C#, and am looking for a way to make my object code reusable instead of having to copy/paste similar code blocks for a large number of items.

Presently I'm working with RatingBar objects on a control. I have about 20 of them with nearly identical behavior. Unfortunately, having neither knowledge of C# or Xamarin, I'm having trouble forming a Google search that's leading me to useful results.

Here's a sample of two similar items in the layout:

'''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" />

'''

And the associated code in the Activity:

'''C#

    RatingBar death = FindViewById<RatingBar>(Resource.Id.arcanadeath);
    death.Rating = thisMage.ArcanaDeath;
    death.RatingBarChange += (sender, e) =>
    {
        thisMage.ArcanaDeath = (int)death.Rating;
        conn.Update(thisMage);
    };

    RatingBar fate = FindViewById<RatingBar>(Resource.Id.arcanafate);
    fate.Rating = thisMage.ArcanaFate;
    fate.RatingBarChange += (sender, e) =>
    {
        thisMage.ArcanaFate = (int)fate.Rating;
        conn.Update(thisMage);
    };

'''

If anyone could tell me how to wrap this up into a class, or reuseable object of some kind - or direct me to what I should be searching for to understand how to do this, I would greatly appreciate it.

Thank you,

xodusprime
  • 21
  • 3

1 Answers1

1

you could create a simple custom components like this:

create 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);
    }

}

create an intetface RateChangeListener.cs :

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

then when you add to activity's layout axml:

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

 />
 ...

in activity code:

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();
    }
}
Leo Zhu
  • 15,726
  • 1
  • 7
  • 23