2012-06-11 15 views
0

我目前在MonoDroid中遇到SeekBar類的問題。如何在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命名空間的其他組件似乎工作得很好,所以我想知道爲什麼這個不起作用。

+0

我認爲有,你可以明確地實現一些其他的構造,你嘗試過嗎? – Cheesebaron

回答

1

剛剛在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: Error when calling constructor of custom view - TwoDScrollView

+0

謝謝!實現其他構造函數完美地工作。 –