ObservableCollection Property Class

本文关键字:Class Property ObservableCollection | 更新日期: 2023-09-27 17:51:11

我在我的MVVM项目中重复这个属性太多次了。创建泛型类或工厂来替换这些代码行的正确方法是什么?

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

ObservableCollection Property Class

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

与泛型类相比,我更喜欢使用这个类,因为它将所有相关代码保存在一个地方,并且很容易理解正在发生的事情。通常我把我所有的私人字段在我的类的顶部,并隐藏所有的公共属性在#region标签,我保持折叠,所以我不需要滚动他们。

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

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

  • Ctrl + C / Ctrl + V 复制/粘贴
  • Ctrl+右/Ctrl+左移动到单词
  • Home/End移动到行首或行尾
  • Shift在移动光标时高亮显示单词

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

这是一个例子宏我用来建立我的所有公共属性的MVVM。它使用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 ObservableCollection<SomeObject> _someCollection;

Ctrl+RE构建公共属性定义

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+RE将游标放在私有字段定义的末尾,因此这是运行该宏时游标需要的位置)

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

如果你需要它,它看起来像这样(注意宏的名字-你需要在运行这个宏之前突出显示公共属性的名字):

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; 
    }
}

如果没有某种形式的代码生成,这是不可能的。查看T4模板,如下所述.

public abstract class XBase<T>
{
    ObservableCollection<T> _resultCollection;
    public ObservableCollection<T> ResultCollection
    {
        get
        {
            if (_resultCollection == null)
                _resultCollection = new ObservableCollection<T>();
            return _resultCollection;
        }
        set
        {
            _resultCollection = value;
        }
    }
}

是…能够绑定到一个领域将是伟大的,不是吗?这让我想到了第一个解决方案:

(1)创建一个允许您绑定字段的转换器。

public class FieldBindingConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var par = parameter as string;
        var field = value.GetType().GetField(par);
        return field.GetValue(value);
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

,然后(2)直接从XAML绑定字段:

    <ItemsControl ItemsSource="{Binding ., Converter={StaticResource Field}, ConverterParameter=Coll2}" Grid.Row="1">
老实说,我有点不喜欢这个,因为它并没有真正弄清楚事情……因此,作为第二种解决方案,您可以直接从构造函数实例化该属性:
public Foo() 
{
    ResultCollection = new ObservableCollection<Result>();
}
[...]
public ObservableCollection<Result> ResultCollection { get; private set; }

虽然这确实会增加几个字节的内存占用(它将惰性实例化的语义更改为直接实例化),但我不会太担心。