WPF新手之自制Snippet —— INotifyPropertyChanged

自己做了自己用,参照了MSDN的标准写法,就不多废话了,直接贴上,最后附用法:

 

先贴类的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比较好。

你可能感兴趣的:(function,String,null,Class,WPF,encoding)