【问题标题】:How to properly subclass SeekBar in MonoDroid?如何在 MonoDroid 中正确继承 SeekBar?
【发布时间】:2012-06-11 13:24:03
【问题描述】:

我目前遇到了 MonoDroid 中的 SeekBar 类的问题。

目前我已经这样扩展了它:

 public class TTSeekBar : SeekBar, ITTComponent
    {
        public TTSeekBar(Context context): base(context)
        {

        }

        private int _min = 0;
        public int Min { get { return _min; } set { _min = value;} }

        public override int Progress
        {
            get
            {
                return base.Progress + _min;
            }
            set
            {
                base.Progress = value;
            }
        }

        public override int Max
        {
            get
            {
                return base.Max + _min;
            }
            set
            {
                base.Max = value + _min;
            }
        }

        public object GetInputData()
        {
            return (this.Progress + _min).ToString();
        }
    }

但是每当我尝试使用TTSeekBar _seekBar = new TTSeekBar(this);(其中this 是一个活动)创建一个对象时,我都会收到一个Sytem.NotSupportedException 向构造函数抛出的消息

无法从本机激活 TTApp.TTSeekBar 类型的实例 处理 44fdad20

像这样扩展 Android.Widget 命名空间的其他组件似乎工作得很好,所以我想知道为什么这个不起作用。

【问题讨论】:

  • 我认为还有一些其他的构造函数可以显式实现,你试过了吗?

标签: c# android .net xamarin.android seekbar


【解决方案1】:

刚刚在 API Level 8 上进行了测试,它似乎可以工作。

using System;
using System.Globalization;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Util;
using Android.Widget;
using Android.OS;

namespace AndroidApplication1
{
    [Activity(Label = "AndroidApplication1", MainLauncher = true, Icon = "@drawable/icon")]
    public class Activity1 : Activity
    {
        int count = 1;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button button = FindViewById<Button>(Resource.Id.MyButton);

            button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };

            var seekbar = new TTSeekBar(this);

            var ll = FindViewById<LinearLayout>(Resource.Id.LinearLayout);

            ll.AddView(seekbar);
        }
    }

    public class TTSeekBar : SeekBar
    {
        protected TTSeekBar(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
        {
        }

        public TTSeekBar(Context context) : base(context)
        {
        }

        public TTSeekBar(Context context, IAttributeSet attrs) : base(context, attrs)
        {
        }

        public TTSeekBar(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
        {
        }

        private int _min = 0;
        public int Min { get { return _min; } set { _min = value; } }

        public override int Progress
        {
            get
            {
                return base.Progress + _min;
            }
            set
            {
                base.Progress = value;
            }
        }

        public override int Max
        {
            get
            {
                return base.Max + _min;
            }
            set
            {
                base.Max = value + _min;
            }
        }

        public object GetInputData()
        {
            return (Progress + _min).ToString(CultureInfo.InvariantCulture);
        }
    }
}

所以正如我所说,您只需要实现正确的构造函数,它应该可以正常工作。

这里有一个解释:MonoDroid: Error when calling constructor of custom view - TwoDScrollView

【讨论】:

  • 谢谢!实现其他构造函数效果很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-15
  • 1970-01-01
  • 2010-11-17
  • 2011-05-03
  • 2011-04-19
  • 1970-01-01
  • 2014-10-11
相关资源
最近更新 更多