我創造了這個代碼段用於此目的(例如):
private string _Name;
public string Name
{
get { return _Name; }
set
{
if (_Name != value) {
_Name = value;
OnPropertyChanged("Name");
}
}
}
段:
<?xml version="1.0"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>propn (Creates a notifying property)</Title>
<Shortcut>propn</Shortcut>
<Description>This snippet helps implementing INotifyPropertyChanged by creating a property with backing store. The settter calls OnPropertyChanged if the value changes. Use the "notify" code snippet in order to implement INotifyPropertyChanged.</Description>
<Author>Olivier Jacot-Descombes</Author>
</Header>
<Snippet>
<Declarations>
<Literal Editable="false">
<ID>classname</ID>
<ToolTip>Name of class</ToolTip>
<Default>ClassNamePlaceholder</Default>
<Function>ClassName()</Function>
</Literal>
<Literal Editable="true">
<ID>NameOfProperty</ID>
<ToolTip>
</ToolTip>
<Default>MyProp</Default>
<Function>
</Function>
</Literal>
<Literal Editable="true">
<ID>Type</ID>
<ToolTip>
</ToolTip>
<Default>int</Default>
<Function>
</Function>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[private $Type$ _$NameOfProperty$;
public $Type$ $NameOfProperty$
{
get { return _$NameOfProperty$; }
set
{
if (_$NameOfProperty$ != value) {
_$NameOfProperty$ = value;
OnPropertyChanged("$NameOfProperty$");
}
}
}
]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
複製這段代碼放到一個文本文件中。給它擴展名「.snippet」,你可以使用代碼段管理器(工具菜單)將它導入到Visual Studio中。
在這裏,它創建的事件處理程序(例如)另一片段:
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
//TODO: Inherit interface System.ComponentModel.INotifyPropertyChanged.
//TODO: Create properties with the propn code snippet.
private void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
段:
<?xml version="1.0"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
<Title>notify (Implements INotifyPropertyChanged)</Title>
<Shortcut>notify</Shortcut>
<Description>This snippet the inserts the INotifyPropertyChanged implementation into a class</Description>
<Author>Olivier Jacot-Descombes</Author>
</Header>
<Snippet>
<Declarations>
<Literal Editable="false">
<ID>classname</ID>
<ToolTip>Name of class</ToolTip>
<Default>ClassNamePlaceholder</Default>
<Function>ClassName()</Function>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
//TODO: Inherit interface System.ComponentModel.INotifyPropertyChanged.
//TODO: Create properties with the propn code snippet.
private void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion]]></Code>
</Snippet>
</CodeSnippet>
<CodeSnippet Format="1.0.0">
<Header>
<Title>
</Title>
<Shortcut>
</Shortcut>
<Description>
</Description>
<Author>
</Author>
</Header>
<Snippet>
<Code Language="csharp"><![CDATA[]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
這些片段是幹什麼的?你能提供一個C#示例嗎? – sll
@sll:我將exmaples添加到了我原來的答案中。 –