c# - ObservableCollection 属性类

标签 c# asp.net generics mvvm

我在我的 MVVM 中重复此属性项目太多了,数不过来。创建通用类或工厂来替换这些代码行的正确方法是什么?

    ObservableCollection<Result> _resultCollection;
    public ObservableCollection<Result> ResultCollection
    {
        get
        {
            if (_resultCollection == null)
                _resultCollection = new ObservableCollection<Result>();
            return _resultCollection;
        }
        set
        {
            _resultCollection = value;
        }
    }

最佳答案

我知道这并不能完全回答您的问题,但是我个人更喜欢在 Visual Studio 中使用录制的宏来为我编写所有这些。

我更喜欢使用它而不是泛型类,因为它将所有相关代码保存在一个地方,并且很容易理解发生了什么。通常,我将所有私有(private)字段放在类(class)顶部,并将所有公共(public)属性隐藏在 #region 中。我一直折叠的标签,所以我不需要滚动它们。

在 VS 中创建宏相当容易:只需转到工具 > 宏 > 录制临时宏,然后仅使用键盘执行您想要的更改。让宏正常工作后,只需将其保存为永久宏即可。如果你做对了,你可以用任何变量重新运行宏,它会以同样的方式构建它。

创建宏时要记住的一些有用的键盘快捷键是:

  • Ctrl+C/Ctrl+V 复制/粘贴
  • Ctrl+Right/Ctrl+Left 移动单词
  • Home/End 移动到行首或行尾
  • 移动光标时突出显示单词的 Shift

  • 您可能还需要对 VB 宏代码进行一些小的修改,例如 .Replace()将小写字母变为大写字母。

    这是一个示例宏,我用它来构建我所有的 MVVM 公共(public)属性。它使用 PRISM 库,因此它使用语法 RaisePropertyChanged(() => this.SomeProperty);
    Sub PRISM_BuildPropertyChanged_CursorAtDefaultAfterCtrlRE()
        DTE.ActiveDocument.Selection.LineDown(False, 2)
        DTE.ActiveDocument.Selection.WordLeft(True)
        DTE.ActiveDocument.Selection.Copy()
        DTE.ActiveDocument.Selection.LineDown(False, 3)
        DTE.ActiveDocument.Selection.EndOfLine()
        DTE.ActiveDocument.Selection.CharLeft()
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "RaisePropertyChanged(() => this."
        DTE.ActiveDocument.Selection.Paste()
        DTE.ActiveDocument.Selection.Text = ");"
        DTE.ActiveDocument.Selection.LineUp()
        DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)
        DTE.ActiveDocument.Selection.CharRight(False, 4)
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.CharRight()
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "if (value != "
        DTE.ActiveDocument.Selection.WordRight(True)
        DTE.ActiveDocument.Selection.Copy()
        DTE.ActiveDocument.Selection.CharLeft()
        DTE.ActiveDocument.Selection.Paste()
        DTE.ActiveDocument.Selection.DeleteLeft()
        DTE.ActiveDocument.Selection.Text = ")"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "{"
        DTE.ActiveDocument.Selection.LineDown()
        DTE.ActiveDocument.Selection.EndOfLine()
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "}"
    End Sub
    

    有了它,我可以写我的私有(private)定义,比如
    private ObservableCollection<SomeObject> _someCollection;
    

    按 Ctrl+R、E 构建公共(public)属性定义
    private ObservableCollection<SomeObject> _someCollection;
    
    public ObservableCollection<SomeObject> SomeCollection
    {
        get { return _someCollection; }
        set { _someCollection = value; }
    }
    

    然后运行我的宏来构建属性更改通知
    public ObservableCollection<SomeObject> SomeCollection
    {
        get { return _someCollection; }
        set 
        {
            if (value != _someCollection)
            {
                _someCollection = value;
                RaisePropertyChanged(() => this.SomeCollection);
            }
        }
    }
    

    (默认情况下,Ctrl+R、E 将光标放在私有(private)字段定义的末尾,因此这是运行此宏时光标需要位于的位置)

    我还有另一个旧宏,它添加了检查值是否为空的代码,如果是,则将其设置为对象的新实例,但我从未使用过太多,因为我更喜欢在构造函数中设置默认定义而不是在属性定义中。

    如果需要,它看起来像这样(注意宏名称 - 您需要在运行此宏之前突出显示公共(public)属性名称):
    Sub CreatePublicGet_NoNull_HightlightPropertyNameFirst()
        DTE.ActiveDocument.Selection.CharLeft(False, 2)
        DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText, True)
        DTE.ActiveDocument.Selection.WordRight(True)
        DTE.ActiveDocument.Selection.Copy()
        DTE.ActiveDocument.Selection.LineDown(False, 2)
        DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)
        DTE.ActiveDocument.Selection.CharRight(False, 4)
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.CharRight()
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.EndOfLine()
        DTE.ActiveDocument.Selection.CharLeft()
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.LineUp(False, 2)
        DTE.ActiveDocument.Selection.EndOfLine()
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Paste()
        DTE.ActiveDocument.Selection.Text = "();"
        DTE.ActiveDocument.Selection.LineDown()
        DTE.ActiveDocument.Selection.WordLeft(True)
        DTE.ActiveDocument.Selection.CharLeft()
        DTE.ActiveDocument.Selection.WordLeft(True)
        DTE.ActiveDocument.Selection.Copy()
        DTE.ActiveDocument.Selection.LineUp(False, 2)
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "if ("
        DTE.ActiveDocument.Selection.Paste()
        DTE.ActiveDocument.Selection.Text = " == null)"
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "{"
        DTE.ActiveDocument.Selection.LineDown()
        DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)
        DTE.ActiveDocument.Selection.EndOfLine()
        DTE.ActiveDocument.Selection.NewLine()
        DTE.ActiveDocument.Selection.Text = "}"
        DTE.ActiveDocument.Selection.LineUp()
        DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)
        DTE.ActiveDocument.Selection.Paste()
        DTE.ActiveDocument.Selection.Text = " = new "
        DTE.ActiveDocument.Selection.Collapse()
        DTE.ActiveDocument.Selection.EndOfLine()
        DTE.ActiveDocument.Selection.LineDown(False, 4)
        DTE.ActiveDocument.Selection.EndOfLine()
        DTE.ActiveDocument.Selection.LineUp(True)
        DTE.ActiveDocument.Selection.Delete()
    End Sub
    

    并导致代码如下所示:
    public ObservableCollection<SomeObject> SomeCollection
    {
        get 
        {
            if (_someCollection == null)
            {
                _someCollection = new ObservableCollection<SomeObject>();
            }
            return _someCollection; 
        }
    }
    

    关于c# - ObservableCollection 属性类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14261344/

    相关文章:

    c# - FastMember 用法

    javascript - 文本输入如何仅接受正整数

    c# - 如何从 USB token (etoken pro 72 k(Java))读取证书并附加到 pdf

    asp.net - 服务器端 Blazor 与 MVC

    java - 如何在通用静态方法中获取类型的字符串表示形式

    c# - 有没有通用的方法来填充数据库中的对象?

    c# - Linq 获取不同的

    c# - C# 中的 AbstractPreferences 等价物

    c# - 为什么从 <T> 到 <U> 的隐式转换运算符接受 <T?>?

    c# - 绑定(bind)DataSource时如何防止selectedindexchanged事件?