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,