当我的 SeekBar 进度属性更改时,我需要通知!我创建了我的搜索栏并覆盖进度属性!但它不起作用!
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));
}
}
}
有些东西在你的项目中没有加起来。如果您使用的是布局,那么也许您忘记将 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,因此控件应为 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>