在WPF中实现INotifyPropertyChanged接口的辅助snippets
来源:互联网 发布:win10卡顿如何优化 编辑:程序博客网 时间:2024/04/28 11:53
http://www.cnblogs.com/alexou/archive/2009/10/17/1585288.html
在Silverlight与WPF的开发过程中,为了使用Binding技术,往往要将自己的实体类实现INotifyPropertyChanged接口。但为每一个Property书写调用接口,并不是省时省力的事情,而且在硬编码属性的NAME,也可能会在以后的重构过程中引入潜在的Bug。在参考了下面两篇文章后,使用Lambda表达式与snippet快速的完成实体类的属性声明。
http://www.jphamilton.net/blog/post/MVVM-with-Type-Safe-INotifyPropertyChanged.aspx
http://www.designerwpf.com/2009/04/30/inotifypropertychanged-snippets-and-why-you-should-use-these-instead-of-dependencyproperties/
重要的是two-way绑定数据时,使用IPropertyChanged(针对standalone data),而不是DependencyProperty(这个针对Control)
**********************
It turns out I had gotten confused. I understood that DependencyProperties were good for the following:
- Providing callbacks when the property is changed
- Binding to stuff
- Animations
I figured that this is the kind of behavior I wanted from my data. I was wrong. As it turns out that is the kind of behavior I want out of the properties that I use in my WPF and Silverlight controls. It seems that DependencyProperties are meant to be used with controls and not for stand-alone data.
For stand-alone data, I should have used INotifyPropertyChanged, which is an interface for… well… notifying things when a property changes.
**********************
1 public abstract class ViewModel<MODEL> : INotifyPropertyChanged
2 {
3 public event PropertyChangedEventHandler PropertyChanged;
4 protected void RaisePropertyChanged<R>(Expression<Func<MODEL, R>> x)
5 {
6 var body = x.Body as MemberExpression;
7 if (body == null)
8 throw new ArgumentException("请输入属性的引用");
9 string propertyName = body.Member.Name;
10 PropertyChangedEventHandler handler = this.PropertyChanged;
11 if (handler != null)
12 {
13 var e = new PropertyChangedEventArgs(propertyName);
14 handler(this, e);
15 }
16 }
17 }
首先定义基类,所有的实体类,均需从ViewModel<>类派生出来。
之后,创建一个snippet。将下面的代码,另存为INotifyProperty.snippet,并保存到Visual Studio的相应目录中,例如我的VS2008中,目录为:C:/Program Files/Microsoft Visual Studio 9.0/VC#/Snippets/2052/Visual C#
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>INotifyPropertyChanged Property: Use this to create a new property INotifyPropertyChanged implementation.</Title>
<Shortcut>notifyp</Shortcut>
<Description>This class must inherited from ViewModel or implements RaisePropertyChanged method.</Description>
<Author>alexou</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>PropertyName</ID>
<ToolTip>The name of the private property</ToolTip>
<Default>MyProperty</Default>
</Literal>
<Literal>
<ID>type</ID>
<ToolTip>The type of the property (e.g. string, double, bool, Brush, etc.)</ToolTip>
<Default>string</Default>
</Literal>
</Declarations>
<Code Language="csharp">
<!--[CDATA[
private $type$ _$PropertyName$;
public $type$ $PropertyName$
{
get{ return _$PropertyName$;}
set
{
_$PropertyName$ = value;
RaisePropertyChanged(item => item.$PropertyName$);
}
}
$end$]]-->
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
现在开始声明自己的实体类,举个简单的例子:声明一个Person类,里面应当由诸如Name,Age,Sex,Phone等通用属性,只需要按照下面的方式,就可以非常快速并且安全的声明相应的成员变量和INotifyPropertyChanged接口的调用方法。
{
notifyp
//->这里按TAB键,输入属性名和属性类型,如Name, String
}
//就会自动生成
public class Person : ViewModel<Person>
{
private string _Name;
public string Name
{
get { return _Name; }
set
{
_Name = value;
RaisePropertyChanged(item => item.Name);
}
}
}
- 在WPF中实现INotifyPropertyChanged接口的辅助snippets
- 在WPF中实现INotifyPropertyChanged接口的辅助snippets
- WPF MVVM之INotifyPropertyChanged接口的几种实现方式
- WPF INotifyPropertyChanged接口
- wpf INotifyPropertyChanged的用法
- [WPF/Silverlight]INotifyPropertyChanged的实现如何更优雅
- [WPF/Silverlight]让INotifyPropertyChanged的实现更优雅一些
- WPF进阶之接口:INotifyPropertyChanged,ICommand
- WPF进阶之接口:INotifyPropertyChanged,ICommand
- 如何实现INotifyPropertyChanged接口(C#)
- 谈谈 INotifyPropertyChanged 的实现
- 谈谈 INotifyPropertyChanged 的实现
- Silverlight中OneTime,OneWay,TwoWay及INotifyPropertyChanged 接口的理解
- Silverlight中OneTime,OneWay,TwoWay及INotifyPropertyChanged 接口的理解
- vs中无法导航到INotifyPropertyChanged接口的解决办法
- INotifyPropertyChanged接口的简单使用
- WPF中INotifyPropertyChanged用法与数据绑定
- WPF中INotifyPropertyChanged用法与数据绑定
- c# winform socket网络编程,点对点传输文件,socket文件传输,监听端口
- Oracle常见SQL分页实现方案介绍
- 操作系统原理及设计实现
- 经典的博客
- 关于回发或回调参数无效,在配置中使用
- 在WPF中实现INotifyPropertyChanged接口的辅助snippets
- 回调函数例子
- myeclipse8集成jbpm4方法,绝对可以用
- apache下提示undefined function mssql_connect
- 如何配置ubuntu服务器用于embedded-linux的开发
- 多线程的魔力
- UDP通讯方式及编程实现步骤
- flex3 datagrid里面checkbox实现全选功能
- HDU 1296 (表达式求值)