簡體   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