为 Xamarin Android 控件创建自定义对象包装器 [英] Creating a custom object wrapper for Xamarin Android controls
问题描述
我是 Xamarin 和 C# 的新手,正在寻找一种方法来使我的目标代码可重用,而不必为大量项目复制/粘贴类似的代码块.
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.
目前我正在使用一个控件上的 RatingBar 对象.我有大约 20 个行为几乎相同的人.不幸的是,我既不了解 C# 也不了解 Xamarin,因此无法通过 Google 搜索获得有用的结果.
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" />
'''
以及Activity中的相关代码:
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.
谢谢,
推荐答案
你可以像这样创建一个简单的自定义组件:
you could create a simple custom components like this:
创建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 时:
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"
/>
...
在活动代码中:
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();
}
}
这篇关于为 Xamarin Android 控件创建自定义对象包装器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!