【发布时间】:2019-09-02 19:15:20
【问题描述】:
我是 Xamarin 和 C# 的新手,我正在寻找一种方法来使我的目标代码可重用,而不必为大量项目复制/粘贴类似的代码块。
目前我正在使用控件上的 RatingBar 对象。我有大约 20 个行为几乎相同。不幸的是,由于既不了解 C# 也不了解 Xamarin,因此我无法构建能够引导我获得有用结果的 Google 搜索。
这是布局中两个相似项目的示例:
'''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中的相关代码:
'''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);
};
'''
如果有人能告诉我如何将它包装到一个类或某种可重用对象中 - 或者指导我应该寻找什么以了解如何做到这一点,我将不胜感激。
谢谢,
【问题讨论】:
-
google "xamarin android 自定义控件'
-
谢谢@Jason。我浏览了第一页的点击率,但由于我对 Xamarin 缺乏经验,我没有找到可以帮助我达到我需要的水平的项目。
-
@xodusprime 然后用谷歌搜索你不理解的术语,直到你了解你理解的术语......
-
可以吗?