有没有办法在 MVVM 中自动创建 DependencyProperty
Is there a way to automatically create DependencyProperty in MVVM
更多的是可用性问题。
我正在构建一个大型 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,但找不到可以自动为我执行此操作的命令。
有 propdp
片段。只需键入 propdp
并按 TAB 键两次。附加属性也存在类似的片段:propa
如果您使用 ReSharper,dependencyProperty
片段会生成它。在没有 ReSharper 的情况下,propdp
片段正在创建相同的片段。
我创建了一些片段来创建 read/write 和包含更改事件处理程序的只读依赖属性。如果不需要更改事件处理程序,则只需将 -select 从事件处理程序的右括号拖动到逗号,然后再将事件处理程序传递到 属性 元数据并键入 ));
.
这是 read/write 版本
<?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>
更多的是可用性问题。 我正在构建一个大型 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,但找不到可以自动为我执行此操作的命令。
有 propdp
片段。只需键入 propdp
并按 TAB 键两次。附加属性也存在类似的片段:propa
如果您使用 ReSharper,dependencyProperty
片段会生成它。在没有 ReSharper 的情况下,propdp
片段正在创建相同的片段。
我创建了一些片段来创建 read/write 和包含更改事件处理程序的只读依赖属性。如果不需要更改事件处理程序,则只需将 -select 从事件处理程序的右括号拖动到逗号,然后再将事件处理程序传递到 属性 元数据并键入 ));
.
这是 read/write 版本
<?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>