更多可用性问题。 我正在构建一个大型的MVVM应用程序,我发现自己在这里复制/修改了这段代码:
public NodeKind Kind
{
get { return (NodeKind)this.GetValue(KindProperty); }
set { this.SetValue(KindProperty, value); }
}
public static readonly DependencyProperty KindProperty = DependencyProperty.Register(
"Kind", typeof(NodeKind), typeof(DashboardNode));
Visual Studio中是否有一种方法可以使用快捷方式生成此代码?我有Resharper和VS 2015,我找不到能自动为我做的命令。
答案 0 :(得分:9)
如果您使用ReSharper,dependencyProperty
代码段会生成此代码。如果没有ReSharper,propdp
代码段就会创建相同的内容。
答案 1 :(得分:4)
有propdp
代码段。只需输入propdp
并按两次TAB即可。附加属性也存在类似的代码段:propa
答案 2 :(得分:2)
我创建了一些片段来创建包含更改事件处理程序的读/写和只读依赖项属性。如果不需要更改事件处理程序,则只需在将事件处理程序传递到属性元数据并键入));
之前,从事件处理程序的右括号中拖动选择到逗号。
这里是读/写版本
<?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>DependencyProperty</Title>
<Author>will</Author>
<Description>DependencyProperty
</Description>
<HelpUrl>
</HelpUrl>
<Shortcut>dp</Shortcut>
</Header>
<Snippet>
<Declarations>
<Literal Editable="true">
<ID>PropName</ID>
<ToolTip>Property name</ToolTip>
<Default>PropertyName</Default>
<Function>
</Function>
</Literal>
<Literal Editable="false">
<ID>ClassName</ID>
<ToolTip>Class name</ToolTip>
<Default>ClassName</Default>
<Function>ClassName()</Function>
</Literal>
<Literal Editable="true">
<ID>Type</ID>
<ToolTip>Property type</ToolTip>
<Default>object</Default>
<Function>
</Function>
</Literal>
<Literal Editable="true">
<ID>DefaultValue</ID>
<ToolTip>Default value</ToolTip>
<Default>null</Default>
<Function>
</Function>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[#region $PropName$
/// <summary>
/// The <see cref="DependencyProperty"/> for <see cref="$PropName$"/>.
/// </summary>
public static readonly DependencyProperty $PropName$Property =
DependencyProperty.Register(
$PropName$PropertyName,
typeof($Type$),
typeof($ClassName$),
new UIPropertyMetadata($DefaultValue$, On$PropName$PropertyChanged));
/// <summary>
/// Called when the value of <see cref="$PropName$Property"/> changes on a given instance of <see cref="$ClassName$"/>.
/// </summary>
/// <param name="d">The instance on which the property changed.</param>
/// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
private static void On$PropName$PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as $ClassName$).On$PropName$Changed(e.OldValue as $Type$, e.NewValue as $Type$);
}
/// <summary>
/// Called when <see cref="$PropName$"/> changes.
/// </summary>
/// <param name="oldValue">The old value</param>
/// <param name="newValue">The new value</param>
private void On$PropName$Changed($Type$ oldValue, $Type$ newValue)
{
;
}
/// <summary>
/// The name of the <see cref="$PropName$"/> <see cref="DependencyProperty"/>.
/// </summary>
public const string $PropName$PropertyName = "$PropName$";
/// <summary>
/// $end$
/// </summary>
public $Type$ $PropName$
{
get { return ($Type$)GetValue($PropName$Property); }
set { SetValue($PropName$Property, value); }
}
#endregion ]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
只读版本
<?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>
<AlternativeShortcuts>
<Shortcut Value="rodp">Read Only Dependency Property</Shortcut>
</AlternativeShortcuts>
<Title>Readonly DependencyProperty</Title>
<Author>Will Sullivan</Author>
<Description>Readonly DependencyProperty</Description>
<HelpUrl>
</HelpUrl>
<Shortcut>RODP</Shortcut>
</Header>
<Snippet>
<References>
</References>
<Imports>
<Import>
<Namespace>System.Windows</Namespace>
</Import>
</Imports>
<Declarations>
<Literal Editable="false">
<ID>ClassName</ID>
<ToolTip>The class name</ToolTip>
<Default>ClassName</Default>
<Function>ClassName()</Function>
</Literal>
<Literal Editable="true">
<ID>Type</ID>
<ToolTip>Property type</ToolTip>
<Default>object</Default>
<Function>
</Function>
</Literal>
<Literal Editable="true">
<ID>PropName</ID>
<ToolTip>Property name</ToolTip>
<Default>PropertyName</Default>
<Function>
</Function>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[#region $PropName$
/// <summary>
/// The <see cref="DependencyPropertyKey"/> for $PropName$.
/// </summary>
private static readonly DependencyPropertyKey $PropName$Key
= DependencyProperty.RegisterReadOnly(
$PropName$PropertyName,
typeof($Type$),
typeof($ClassName$),
new PropertyMetadata());
/// <summary>
/// The <see cref="DependencyProperty"/> for $PropName$.
/// </summary>
public static readonly DependencyProperty $PropName$Property
= $PropName$Key.DependencyProperty;
/// <summary>
/// The name of the <see cref="$PropName$"/> <see cref="DependencyProperty"/>.
/// </summary>
public const string $PropName$PropertyName = "$PropName$";
/// <summary>
/// $end$
/// </summary>
public $Type$ $PropName$
{
get { return ($Type$)GetValue($PropName$Property ); }
private set { SetValue($PropName$Key, value); }
}
#endregion
]]></Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>