WPF新手之自制Snippet —— INotifyPropertyChanged
2010-09-23 22:26
1026 查看
自己做了自己用,参照了MSDN的标准写法,就不多废话了,直接贴上,最后附用法:
先贴类的Snippet——快捷键为notify
再贴成员变量的Snippet ——快捷键为nn
用法:在VS2010中(08估计也一样)中先键入notify,然后按tab,然后改一下类名,完了按回车,就在回车停下的地方按nn,然后按tab,按顺序改一堆东西,有类型等等,记住按顺序,因为后面的依赖前面的。
默认生成的类:
为什么我要做成两个Snippet呢?因为很可能一个INotifyPropertyChanged类中有好多个变量,这样模块化Snippet比较好。
先贴类的Snippet——快捷键为notify
<?xml version="1.0" encoding="utf-8" ?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <Title>notify</Title> <Shortcut>notify</Shortcut> <Description>Code snippet for System.Diagnostics.Debug.WriteLine</Description> <Author>Microsoft Corporation</Author> <SnippetTypes> <SnippetType>Expansion</SnippetType> </SnippetTypes> </Header> <Snippet> <Declarations> <Literal Editable="true"> <ID>name</ID> <ToolTip>class name</ToolTip> <Default>MyClass</Default> <Function> </Function> </Literal> </Declarations> <Code Language="csharp"><!--[CDATA[class $name$ : INotifyPropertyChanged { $end$ public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } } ]]--> </Code> </Snippet> </CodeSnippet> </CodeSnippets>
再贴成员变量的Snippet ——快捷键为nn
<?xml version="1.0" encoding="utf-8"?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <SnippetTypes> <SnippetType>Expansion</SnippetType> </SnippetTypes> <Title>nn</Title> <Author>Microsoft Corporation</Author> <Description>Code snippet for property and backing field and INotifyPropertyChanged</Description> <HelpUrl> </HelpUrl> <Shortcut>nn</Shortcut> </Header> <Snippet> <Declarations> <Literal Editable="true"> <ID>type</ID> <ToolTip>Property type</ToolTip> <Default>int</Default> <Function> </Function> </Literal> <Literal Editable="true"> <ID>property</ID> <ToolTip>Property name</ToolTip> <Default>MyProperty</Default> <Function> </Function> </Literal> <Literal Editable="true"> <ID>field</ID> <ToolTip>The variable backing this property</ToolTip> <Default>myVar</Default> <Function> </Function> </Literal> </Declarations> <Code Language="csharp"><!--[CDATA[private $type$ $field$; public $type$ $property$ { get { return this.$field$;} set { if(this.$field$ != value) { this.$field$ = value; this.NotifyPropertyChanged("$property$"); } } } $end$]]--></Code> </Snippet> </CodeSnippet> </CodeSnippets>
用法:在VS2010中(08估计也一样)中先键入notify,然后按tab,然后改一下类名,完了按回车,就在回车停下的地方按nn,然后按tab,按顺序改一堆东西,有类型等等,记住按顺序,因为后面的依赖前面的。
默认生成的类:
class MyClass : INotifyPropertyChanged { private int myVar; public int MyProperty { get { return this.myVar; } set { if (this.myVar != value) { this.myVar = value; this.NotifyPropertyChanged("MyProperty"); } } } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } }
为什么我要做成两个Snippet呢?因为很可能一个INotifyPropertyChanged类中有好多个变量,这样模块化Snippet比较好。
相关文章推荐
- WPF Binding INotifyPropertyChanged 多线程 深入理解
- WPF INotifyPropertyChanged
- WPF 自定义NotifyPropertyChanged
- WPF/Silverlight序列化可能顺序可能丢失PropertyChanged,造成通知失效
- WPF进阶之接口:INotifyPropertyChanged,ICommand
- [WPF/Silverlight]让INotifyPropertyChanged的实现更优雅一些
- Android官方DataBinding(二):动态数据更新notifyPropertyChanged
- [WPF/Silverlight]让INotifyPropertyChanged的实现更优雅一些
- WPFINotifyPropertyChanged使用技巧
- WPF,textBox默认是失去焦点绑定值才改变,怎么做到输入框值一改变就改变绑定值. Text="{Binding EvaluationContent,UpdateSourceTrigger=PropertyChanged}"
- wpf RadioButton控件的一个bug,onpropertychanged后会修改旧属性的值
- winform + INotifyPropertyChanged + IDataErrorInfo + ErrorProvider实现自动验证功能
- DependencyProperties or INotifyPropertyChanged ?
- An elegant way to implement INotifyPropertyChanged
- Binding to a dictionary in Silverlight with INotifyPropertyChanged
- INotifyPropertyChanged接口的实现
- Android官方DataBinding(二):动态数据更新notifyPropertyChanged
- C# INotifyPropertyChanged用法,监听属性值发生改变
- 【转】【WPF】关于依赖属性的ValidateValueCallback,PropertyChangedCallback和CoerceValueCallback的执行顺序
- INotifyPropertyChanged 接口