2017-06-10 13 views
0

SeekBar Progressプロパティが変更されたときに通知する必要があります。 私はSeekBarを作成し、進行状況のプロパティをオーバーライドしました! しかし、それは動作しません!自分のINotifypropertyChangedがXamarinのAndroidで動作しない

public class MySeekBar : SeekBar,INotifyPropertyChanged 
{ 

    public MySeekBar(Context context) : base(context) 
    { 

    } 

    public override int Progress 
    { 
     get => base.Progress; 
     set { base.Progress = value; OnPropertyChange(); } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void OnPropertyChange([CallerMemberName] string propName = null) 
    { 
     var change = PropertyChanged; 
     if (change != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propName)); 
     }   
    } 
} 
+0

こんにちは。実際にバインドする場所を 'Progress'に追加し、関連する' BindingContext'をどこに追加できますか? (そしてなぜあなたは新しいMainActivityを作成するのですか?それはどこにもありません – woelliJ

+0

あなたはどのライブラリをバインディングに使用していますか?woellijが示しているように、バインディングコードを表示できますか? – apineda

答えて

0

何かがプロジェクトに追加されません。レイアウトを使用している場合は、SeekBarクラスをMySeekBarに変更するのを忘れていた可能性があります。さらに、レイアウトに必要なコンストラクタが不足しています。実装に関しては、以下のようにプロパティをオーバーライドしないでください。

public class MySeekBar : SeekBar, INotifyPropertyChanged 
{ 
    public MySeekBar(Context context) : base(context) 
    { 
     Initialize(); 
    } 

    public MySeekBar(Context context, IAttributeSet attrs) : base (context,attrs)  
    { 
     Initialize(); 
    } 

    public MySeekBar(Context context, IAttributeSet attrs, int defStyle) : base (context, attrs, defStyle) 
    { 
     Initialize(); 
    } 

    private void Initialize() 
    { 
     this.ProgressChanged += (sender, e) => 
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Progress")); 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
} 

レイアウトに追加されました(ベース名前空間がそのように制御がseekb.MySeekBarなければならないSeekBされます)。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <seekb.MySeekBar 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/seekBar1" /> 
</LinearLayout> 
関連する問題