ObservableCollection 属性类 [英] ObservableCollection Property Class
问题描述
我在我的 MVVM
项目中多次重复这个属性.创建通用类或工厂来替换这些代码行的正确方法是什么?
I repeat this property in my MVVM
project too many times to count. What would be the correct way to create a generic class or factory to replace these lines of code?
ObservableCollection<Result> _resultCollection;
public ObservableCollection<Result> ResultCollection
{
get
{
if (_resultCollection == null)
_resultCollection = new ObservableCollection<Result>();
return _resultCollection;
}
set
{
_resultCollection = value;
}
}
推荐答案
我知道这并不能完全回答您的问题,但我个人更喜欢在 Visual Studio 中使用录制的宏,它为我编写了所有内容.
I know this doesn't exactly answer your question, however personally I prefer to use a recorded macro in Visual Studio that writes all that for me.
我更喜欢使用它而不是通用类,因为它将所有相关代码保存在一个地方,并且很容易理解发生了什么.通常,我将所有私有字段放在类的顶部,并将所有公共属性隐藏在 #region
标记中,我一直折叠该标记,因此我不需要滚动浏览它们.
I prefer using this to a generic class as it keeps all the relevant code in one place, and it's easy to understand what is going on. Typically I put all my private Fields at the top of my class, and hide all the public Properties in a #region
tag that I keep collapsed so I don't need to scroll through them.
在 VS 中创建宏相当容易:只需转到工具">宏">记录临时宏",然后仅使用键盘执行所需的更改.一旦您的宏正常工作,只需将其保存为永久宏.如果你做得对,你可以用任何变量重新运行宏,它会以同样的方式构建它.
It's fairly easy to create a macro in VS: Simply go to Tools > Macros > Record Temporary Macro, then perform the changes you want using the keyboard only. Once you have the Macro working correctly, simply save it as a permanent macro. If you do it right, you can re-run the macro with any variable and it will build it out the same way.
创建宏时要记住的一些有用的键盘快捷键是:
Some useful keyboard shortcuts to remember when creating macros are:
- Ctrl+C/Ctrl+V 复制/粘贴
- Ctrl+Right/Ctrl+Left 移动一个词
- Home/End 移动到行首或行尾
- Shift 用于在移动光标时突出显示单词
- Ctrl+C/Ctrl+V to copy/paste
- Ctrl+Right/Ctrl+Left to move over a word
- Home/End to move to the beginning or end of a line
- Shift for highlighting words as you move the cursor
您可能还需要对 VB 宏代码进行一些小的修改,例如使用 .Replace()
将小写字母变为大写.
You may also need to do some minor modifications to the VB macro code, such as a .Replace()
to turn lowercase letters to capitals.
这是我用来为 MVVM 构建所有公共属性的示例宏.它使用 PRISM 库,因此它使用语法 RaisePropertyChanged(() => this.SomeProperty);
Here's an example Macro I use to build out all my public Properties for MVVM. It uses the PRISM library, so it uses the syntax 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
有了它,我就可以写我的私有定义了,比如
With it, I can write my private definition, such as
private ObservableCollection<SomeObject> _someCollection;
点击Ctrl+R、E来构建公共属性定义
Hit Ctrl+R, E to build the public property definition
private ObservableCollection<SomeObject> _someCollection;
public ObservableCollection<SomeObject> SomeCollection
{
get { return _someCollection; }
set { _someCollection = value; }
}
然后运行我的宏来构建属性更改通知
Then run my Macro to build out the Property Change notification
public ObservableCollection<SomeObject> SomeCollection
{
get { return _someCollection; }
set
{
if (value != _someCollection)
{
_someCollection = value;
RaisePropertyChanged(() => this.SomeCollection);
}
}
}
(默认情况下,Ctrl+R、E 将光标放在私有字段定义的末尾,因此这是运行此命令时光标需要位于的位置宏)
(By default, Ctrl+R, E puts the cursor at the end of the private field definition, so that is where the cursor needs to be when running this macro)
我还有另一个旧宏,它添加代码来检查值是否为空,如果是,则将其设置为对象的新实例,但我从未使用过它,因为我更喜欢在构造函数而不是在属性定义中.
I also have another old macro that adds code that checks if the value is null, and if so sets it to a new instance of the object, but its one I never used much since I prefer to set my default definitions in the Constructor instead of in the Property definition.
如果需要,它看起来像这样(注意宏名称 - 在运行此宏之前,您需要突出显示公共属性名称):
If you want it, it looks like this (note the Macro name - You need to highlight the public property name prior to running this macro):
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;
}
}
这篇关于ObservableCollection 属性类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!