自动创建 getter/setter 的代码片段?

发布于 2024-11-08 01:19:34 字数 525 浏览 0 评论 0原文

我曾经使用过我过去看到/发现的一个代码片段,它将我的单个语句变成私有/公共 getter/setter,自从重新安装我的机器以来,我到目前为止无法重复这个发现。

例如:

private string serverSMTP = string.empty;

然后我可以按 Ctrl k + 并将其变成这样:

        private string serverSMTP = string.Empty;
    public string ServerSMTP
    {
        get { return serverSMTP; }
        set
        {
            serverSMTP = value;
            RaisePropertyChanged("ServerSMTP");
        }
    }

关于如何创建一些东西来执行此操作或扩展/片段来为我处理它的任何想法?在较大的项目中,这会节省我很多时间。

I once used a code snippet I saw/found in the past that would turn my single statement into a private/public getter/setter, I've been so far unable to repeat that find since reinstalling my machine.

for example:

private string serverSMTP = string.empty;

I could then press Ctrl k + and turn it into this:

        private string serverSMTP = string.Empty;
    public string ServerSMTP
    {
        get { return serverSMTP; }
        set
        {
            serverSMTP = value;
            RaisePropertyChanged("ServerSMTP");
        }
    }

Any ideas on how I can create something to do that or an extension/snippet to take care of it for me? In larger projects this would save me a lot of time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦情居士 2024-11-15 01:19:34

如果您已经在使用 MVVM Light 框架,您可以安装它附带的代码片段来执行类似的操作。具体来说,“mvvminpc”片段将执行您正在寻找的操作,尽管它不会采用现有的字段声明并将其转换为具有 propertychanged 通知的属性。

http://mvvmlight.codeplex.com/sourcecontrol/最新#Installer/InstallItems/Snippets/CSharp/mvvmInpc.snippet

用于加速添加新属性的代码片段(仅限 Visual Studio):
mvvminpc 向 ViewModel 添加一个新的可绑定属性。
mvvmlocatorproperty 将新的 ViewModel 添加到 ViewModeLocator。
mvvmpropa 向 DependencyObject 添加新的附加属性(仅限 WPF)。
mvvmpropdp 向 DependencyObject 添加新的依赖属性(仅限 WPF)。
mvvmslpropa 向 DependencyObject 添加新的附加属性(仅限 Silverlight)。
mvvmslpropdp 向 DependencyObject 添加新的依赖属性(仅限 Silverlight)。

If you are already using the MVVM Light framework you can install the code snippets that ship with it that will do something similar. Specifically the "mvvminpc" snippet will do what you are looking for, although it will not take an existing field declaration and convert it to a property with a propertychanged notification.

http://mvvmlight.codeplex.com/sourcecontrol/latest#Installer/InstallItems/Snippets/CSharp/mvvmInpc.snippet

Code snippets to speed up the addition of new properties (Visual Studio only):
mvvminpc adds a new bindable property to a ViewModel.
mvvmlocatorproperty adds a new ViewModel to a ViewModeLocator.
mvvmpropa adds a new attached property to a DependencyObject (WPF only).
mvvmpropdp adds a new dependency property to a DependencyObject (WPF only).
mvvmslpropa adds a new attached property to a DependencyObject (Silverlight only).
mvvmslpropdp adds a new dependency property to a DependencyObject (Silverlight only).

放手` 2024-11-15 01:19:34

将此片段放入

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Shortcut>propn</Shortcut>
            <Title>
                Notify Property
            </Title>
        </Header>
        
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>type</ID>
                    <ToolTip>Type</ToolTip>
                    <Default>int</Default>
                </Literal>
                <Literal>
                    <ID>field</ID>
                    <ToolTip>Field name</ToolTip>
                    <Default>fieldName</Default>
                </Literal>
                <Literal>
                    <ID>property</ID>
                    <ToolTip>Propery Name</ToolTip>
                    <Default>PropertyName</Default>
                </Literal>
            </Declarations>

            <Code Language="CSharp">
                <![CDATA[       
private $type$ $field$;
public $type$ $property$
{
    get { return $field$; }
    set
    {
        if($field$ != value)
        {
            $field$ = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("$property$"));
        }
    }
}
$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

以下文件夹中的文件 propn.snippet 中:
C:\Users[YOUR_USERNAME]\Documents\Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets

,之后您将能够使用 (propn + tab + tab) 快捷方式使用此代码片段。

xml 片段非常容易您自己理解,因此您可以轻松地将其调整为您需要的任何内容。

put this snippet:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Shortcut>propn</Shortcut>
            <Title>
                Notify Property
            </Title>
        </Header>
        
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>type</ID>
                    <ToolTip>Type</ToolTip>
                    <Default>int</Default>
                </Literal>
                <Literal>
                    <ID>field</ID>
                    <ToolTip>Field name</ToolTip>
                    <Default>fieldName</Default>
                </Literal>
                <Literal>
                    <ID>property</ID>
                    <ToolTip>Propery Name</ToolTip>
                    <Default>PropertyName</Default>
                </Literal>
            </Declarations>

            <Code Language="CSharp">
                <![CDATA[       
private $type$ $field$;
public $type$ $property$
{
    get { return $field$; }
    set
    {
        if($field$ != value)
        {
            $field$ = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("$property
quot;));
        }
    }
}
$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

inside a file propn.snippet, in this folder:
C:\Users[YOUR_USERNAME]\Documents\Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets

and afterwards you'll be able to use this snippet using the (propn + tab + tab) shortcut.

the snippet xml is very easy to understand on your own, so you can easily adjust it to whatever you need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文