如何正确子类搜索栏在MonoDroid的? [英] How to properly subclass SeekBar in MonoDroid?
问题描述
我目前有在MonoDroid的搜索栏类的问题。
I'm currently having a issue with the SeekBar class in MonoDroid.
目前我已经扩展它是这样的:
Currently I have extended it like this:
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 =新TTSeekBar(本);
(其中这个
是一个活动),我收到了 Sytem.NotSupportedException
在该消息的构造函数抛出
But whenever I try to create a object using TTSeekBar _seekBar = new TTSeekBar(this);
(where this
is a Activity) I get a Sytem.NotSupportedException
thrown at the constructor with the message
无法从本地激活型TTApp.TTSeekBar实例
处理44fdad20
Unable to activate instance of type TTApp.TTSeekBar from native handle 44fdad20
延长 Android.Widget
命名空间的其他组件,如这似乎工作得很好,所以我不知道为什么这一个好好尝试的工作。
Extending other components of the Android.Widget
namespace like this seems to work just fine, so I'm wondering why this one doens't work.
推荐答案
刚上API级别8测试这一点,它似乎工作。
Just tested this on API Level 8 and it seems to work.
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);
}
}
}
因此,正如我说你只需要实现合适的构造函数,它应该工作得很好。
So as I said you just need to implement the right constructors and it should work just fine.
有一个解释,为什么在这里:<一href=\"http://stackoverflow.com/questions/10593022/monodroid-error-when-calling-constructor-of-custom-view-twodscrollview\">MonoDroid:错误调用自定义视图的构造函数时 - TwoDScrollView
There is an explanation as to why here: MonoDroid: Error when calling constructor of custom view - TwoDScrollView
这篇关于如何正确子类搜索栏在MonoDroid的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!