如何实现InotifyPropertyChanged接口
答案:3 悬赏:70 手机版
解决时间 2021-04-18 14:46
- 提问者网友:泪痣哥哥
- 2021-04-18 00:44
如何实现InotifyPropertyChanged接口
最佳答案
- 五星知识达人网友:逃夭
- 2021-04-18 00:53
InotifyPropertyChanged接口实现分一下几个步骤:
1、定义了一个类,实现接口,实现了绑定和数据的同步(代码如下)
public class person : IMyPropertyChanged
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
if (value != _name)
{
_name = value;
//when changed ,notice
NotifyChanged(Name);
}
}
}
}
public class IMyPropertyChanged : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
2、随着定义的person对象属性越来越多,可能有年纪,毕业院校,爱好,性取向等等等,岂不是要写很多类似以上的重复代码?而且,最要命的是,以上方面采用的是硬代码,很容易编译通过,而却出错,我们要采用强类型才行!(专业性较强,2年内程序员的可能看不懂)
public class IMyPropertyChanged : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyChanged(Expression> propertyName)
{
if (PropertyChanged != null)
{
var memberExpression = propertyName.Body as MemberExpression;
if (memberExpression != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(memberExpression.Member.Name));
}
}
}
}
public class person : IMyPropertyChanged
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
if (value != _name)
{
_name = value;
//when changed ,notice
NotifyChanged(() => this.Name);
}
}
}
}
3、我们要封装这个操作,那就定义一个静态类:
public class PropertyChangedBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public static class IMyPropertyChangedEX
{
public static void NotifyPropertyChanged(this T iMyPropertyChanged,
Expression> expression) where T : IMyPropertyChanged
{
var memberExpression = expression.Body as MemberExpression;
if (memberExpression != null)
{
string propertyName = memberExpression.Member.Name;
iMyPropertyChanged.NotifyPropertyChanged(propertyName);
}
else
throw new NotImplementedException();
}
}
4、在person里面编写绑定的属性的更改通知时,就可以这么写:
public class person : IMyPropertyChanged
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
if (value != _name)
{
_name = value;
//when changed ,notice
this.NotifyPropertyChanged(p => p.Name); ---!!!!!!
1、定义了一个类,实现接口,实现了绑定和数据的同步(代码如下)
public class person : IMyPropertyChanged
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
if (value != _name)
{
_name = value;
//when changed ,notice
NotifyChanged(Name);
}
}
}
}
public class IMyPropertyChanged : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
2、随着定义的person对象属性越来越多,可能有年纪,毕业院校,爱好,性取向等等等,岂不是要写很多类似以上的重复代码?而且,最要命的是,以上方面采用的是硬代码,很容易编译通过,而却出错,我们要采用强类型才行!(专业性较强,2年内程序员的可能看不懂)
public class IMyPropertyChanged : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyChanged
{
if (PropertyChanged != null)
{
var memberExpression = propertyName.Body as MemberExpression;
if (memberExpression != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(memberExpression.Member.Name));
}
}
}
}
public class person : IMyPropertyChanged
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
if (value != _name)
{
_name = value;
//when changed ,notice
NotifyChanged
}
}
}
}
3、我们要封装这个操作,那就定义一个静态类:
public class PropertyChangedBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public static class IMyPropertyChangedEX
{
public static void NotifyPropertyChanged
Expression
{
var memberExpression = expression.Body as MemberExpression;
if (memberExpression != null)
{
string propertyName = memberExpression.Member.Name;
iMyPropertyChanged.NotifyPropertyChanged(propertyName);
}
else
throw new NotImplementedException();
}
}
4、在person里面编写绑定的属性的更改通知时,就可以这么写:
public class person : IMyPropertyChanged
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
if (value != _name)
{
_name = value;
//when changed ,notice
this.NotifyPropertyChanged(p => p.Name); ---!!!!!!
全部回答
- 1楼网友:天凉才是好个秋
- 2021-04-18 01:56
印象中好像是实现格式不正确,好久不做C#了,记不清了.
public class Info:INotifyPropertyChanged
在这个的INotifyPropertyChanged上鼠标右键,接口->实现接口,让VS自动帮你生成要实现的函数.自己再补逻辑代码
public class Info:INotifyPropertyChanged
在这个的INotifyPropertyChanged上鼠标右键,接口->实现接口,让VS自动帮你生成要实现的函数.自己再补逻辑代码
- 2楼网友:第幾種人
- 2021-04-18 01:25
public event PropertyChangedEventHandler PropertyChange;
改为:
public event PropertyChangedEventHandler PropertyChanged;
然后在属性更改之后(一定是之后)触发这个事件即可:
public string UserName {
get { return _userName; }
set
{
_userName = value;
NotifyPropertyChanged("UserName");
}
}
ps:属性和方法名首字母要大写
改为:
public event PropertyChangedEventHandler PropertyChanged;
然后在属性更改之后(一定是之后)触发这个事件即可:
public string UserName {
get { return _userName; }
set
{
_userName = value;
NotifyPropertyChanged("UserName");
}
}
ps:属性和方法名首字母要大写
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯