I am using Visual Studio '17 and Resharper in a WPF project that displays a lot of information to the user. Therefore I have a lot of properties that look like the following.
private double _foo;
public double Foo
{
get { return _foo; }
set { SetProperty(ref _foo, value); }
}
I'm looking for a quick automated way to create this. Right now I type 'prop' and get this
public double Foo {get; set;}
hit 'Alt-Enter' -> 'To property with backing field' to get this
private double _foo;
public double Foo
{
get { return _foo; }
set { _foo = value); }
}
and then manually type in the SetProperty(ref and ',' to get the final result. I've tried setting up snippets and resharper Templates but can't seem to figure out how to do it in one operation. What have people come up with to make this easier? Specifically, it would help tremendously if I could change an already existing auto-property to a View Model property.
With my Visual Commander extension, you can use the following C# command (References: Microsoft.VisualBasic) to create a view model property:
With C# 6 it is simpler, but requires Visual Commander Professional:
You can define your own code snippet, to suit your needs precisely.
Here's one I wrote for myself, to generate property implementations compatible with the
INotifyPropertyChanged
base class implementation I also use regularly:Use the Visual Studio Snippet Manager to add the snippet to your installation.
(I didn't bother adding a field for the property name itself, because there didn't seem any point to it. But you can, of course, if you like. The only advantage I see vs. just selecting the property name and over-typing it is that if it's a field, you should be able to use the tab key to get directly to it before over-typing.)
U can create your own snippet for that. I've create propbb snippet for BindableBase(from PRISM) property, it's essentialy same as you want. To create snippet here is video i've followed.
https://www.youtube.com/watch?v=tGmXlUFMTpk
and my snippet looks like this
if you want this is my snippet, just include it in your visual studio(like in video i've provided)
propbb snippet download
Path to add snippet in VS2017: C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC#\Snippets\1033\Visual C# Just download snippet from my dropbox and copy there, to use it write
propbb
and tab tabInstall the Prism Template Pack from the Visual Studio Marketplace and then use the "propp" snippet.
https://marketplace.visualstudio.com/items?itemName=BrianLagunas.PrismTemplatePack