物业的ObservableCollection类 [英] ObservableCollection Property Class
问题描述
我重复我的 MVVM
项目这个属性太多次来算。什么是创建一个通用类或原厂更换code,这些线路上的正确方法是什么?
的ObservableCollection<结果> _resultCollection;
公众的ObservableCollection<结果> ResultCollection
{
得到
{
如果(_resultCollection == NULL)
_resultCollection =新的ObservableCollection<结果>();
返回_resultCollection;
}
组
{
_resultCollection =价值;
}
}
我知道这并不完全回答你的问题,但是我个人preFER使用Visual Studio中录制的宏写入一切对我来说。
因为它保持在一个地方所有相关code和很容易明白是怎么回事使用这一个通用的I类preFER。通常情况下,我把我所有的私有字段在我的类的顶部,并隐藏在#地区
标签的所有公共属性,我不断崩溃,所以我并不需要滚动通过他们。
这是相当容易地创建一个VS宏:只需转到工具>宏>录制宏临时,然后执行你只想要使用键盘的变化。一旦你的宏正常工作,只需将其保存为一个永久的宏。如果你这样做是正确的,你可以重新运行任何变量宏将建立它以同样的方式。
一些有用的快捷键时要记住创建宏是:
- <大骨节病>按Ctrl + C 骨节病> / <大骨节病>按Ctrl + V 骨节病>复制/粘贴
- <大骨节病>按Ctrl +右骨节病> / <大骨节病> Ctrl +左骨节病>搬过来一个字
- <大骨节病>首页骨节病> / <大骨节病>结束骨节病>移动到一行的开头或结尾
- <大骨节病>移骨节病>突出显示的话,你将光标移动
您还可能需要做一些小修改的VB宏code,如 .Replace()
把小写字母大写。
下面是一个例子宏我用它来构建出MVVM我所有的公共属性。它使用PRISM库,所以它使用的语法 RaisePropertyChanged(()=&GT; this.SomeProperty);
子PRISM_BuildPropertyChanged_CursorAtDefaultAfterCtrlRE()
DTE.ActiveDocument.Selection.LineDown(假,2)
DTE.ActiveDocument.Selection.WordLeft(真)
DTE.ActiveDocument.Selection.Copy()
DTE.ActiveDocument.Selection.LineDown(假,3)
DTE.ActiveDocument.Selection.EndOfLine()
DTE.ActiveDocument.Selection.CharLeft()
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text =RaisePropertyChanged(()=&gt;这。
DTE.ActiveDocument.Selection.Paste()
DTE.ActiveDocument.Selection.Text =);
DTE.ActiveDocument.Selection.LineUp()
DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)
DTE.ActiveDocument.Selection.CharRight(假,4)
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.CharRight()
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text =如果(值!=
DTE.ActiveDocument.Selection.WordRight(真)
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 =}
结束小组
有了它,我可以写我的私人的定义,如
私人的ObservableCollection&LT; SomeObject&GT; _someCollection;
按<大骨节病>按Ctrl + R 骨节病>,<大骨节病>电子骨节病>构建公共财产的定义
私人的ObservableCollection&LT; SomeObject&GT; _someCollection;公众的ObservableCollection&LT; SomeObject&GT; SomeCollection
{
{返回_someCollection; }
集合{_someCollection =价值; }
}
然后运行我的微距打造出来的属性更改通知
公开的ObservableCollection&LT; SomeObject&GT; SomeCollection
{
{返回_someCollection; }
组
{
如果(值!= _someCollection)
{
_someCollection =价值;
RaisePropertyChanged(()=&GT; this.SomeCollection);
}
}
}
(默认情况下,<大骨节病>按Ctrl + R 骨节病>,<大骨节病>电子骨节病>把光标在私人领域定义的结束,所以这是在哪里游标需要运行这个时宏)
我也有另一位宏,增加了code来检查,如果该值为null,如果是它设置为对象的新实例,但它的一个我从来没有使用过很多,因为我preFER到设置在构造函数,而不是在属性定义我的默认定义。
如果你想要它,它看起来像这样(注意宏的名字 - 你需要突出公共财产的名称运行此宏之前):
子CreatePublicGet_NoNull_HightlightPropertyNameFirst()
DTE.ActiveDocument.Selection.CharLeft(假,2)
DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText,真)
DTE.ActiveDocument.Selection.WordRight(真)
DTE.ActiveDocument.Selection.Copy()
DTE.ActiveDocument.Selection.LineDown(假,2)
DTE.ActiveDocument.Selection.StartOfLine(vsStartOfLineOptions.vsStartOfLineOptionsFirstText)
DTE.ActiveDocument.Selection.CharRight(假,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(假,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(真)
DTE.ActiveDocument.Selection.CharLeft()
DTE.ActiveDocument.Selection.WordLeft(真)
DTE.ActiveDocument.Selection.Copy()
DTE.ActiveDocument.Selection.LineUp(假,2)
DTE.ActiveDocument.Selection.NewLine()
DTE.ActiveDocument.Selection.Text =如果(
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 ==新
DTE.ActiveDocument.Selection.Collapse()
DTE.ActiveDocument.Selection.EndOfLine()
DTE.ActiveDocument.Selection.LineDown(假,4)
DTE.ActiveDocument.Selection.EndOfLine()
DTE.ActiveDocument.Selection.LineUp(真)
DTE.ActiveDocument.Selection.Delete()
结束小组
和结果code看起来像这样:
公开的ObservableCollection&LT; SomeObject&GT; SomeCollection
{
得到
{
如果(_someCollection == NULL)
{
_someCollection =新的ObservableCollection&LT; SomeObject&GT;();
}
返回_someCollection;
}
}
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;
}
}
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.
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.
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 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
You may also need to do some minor modifications to the VB macro code, such as a .Replace()
to turn lowercase letters to capitals.
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;
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);
}
}
}
(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
And results in code looking like this:
public ObservableCollection<SomeObject> SomeCollection
{
get
{
if (_someCollection == null)
{
_someCollection = new ObservableCollection<SomeObject>();
}
return _someCollection;
}
}
这篇关于物业的ObservableCollection类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!