繁体   English   中英

如何正确地在MonoDroid中子类SeekBar?

[英]How to properly subclass SeekBar in MonoDroid?

我目前在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);创建一个对象时, TTSeekBar _seekBar = new TTSeekBar(this); this是一个活动)我得到一个Sytem.NotSupportedException抛出与消息

无法从本地句柄44fdad20激活类型TTApp.TTSeekBar的实例

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

刚刚在API级别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:调用自定义视图的构造函数时出错-TwoDScrollView

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM