【问题标题】:my INotifypropertyChanged doesnt work in my Xamarin Android我的 INotifypropertyChanged 在我的 Xamarin Android 中不起作用
【发布时间】:2017-06-10 08:01:25
【问题描述】:

我需要在我的 SeekBar Progress 属性更改时通知! 我创建了我的 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));
        }          
    }
}

【问题讨论】:

  • 嗨。您可以添加实际绑定到Progress 的位置以及相关BindingContext 设置的位置吗? (你为什么要在那里创建一个新的MainActivity?它无处使用
  • 您使用什么库进行绑定?正如woellij所说,你能显示绑定代码吗?

标签: android xamarin xamarin.android inotifypropertychanged


【解决方案1】:

您的项目中没有添加任何内容。如果您使用的是布局,那么您可能忘记将 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.MySeekBar)。

<?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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-25
    • 1970-01-01
    • 1970-01-01
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    相关资源
    最近更新 更多