【问题标题】:Creating a custom object wrapper for Xamarin Android controls为 Xamarin Android 控件创建自定义对象包装器
【发布时间】: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 然后用谷歌搜索你不理解的术语,直到你了解你理解的术语......
  • 可以吗?

标签: c# xamarin


【解决方案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();
    }
}

【讨论】:

    猜你喜欢
    • 2014-06-14
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    • 2021-10-02
    相关资源
    最近更新 更多