Question

in our architecture, we have a bunch of models like this

public class UserModel
{
    public string FirstName {get;set;}
}

and since we're using MvvmCross for our view models, we need our properties to look like this

public class UserViewModel: MvxViewModel
{
    private string _firstName;
    public string FirstName
    {
        get { return _firstName; }
        set { _firstName = value; RaisePropertyChanged(() => FirstName); }
    }
}

Now I've already got an R# template to write my own mvx properties by simply typing propmvx, but I still need to type in the type and the name.

I'm wondering if there's a way to setup a custom conversion template in order to have the alt + enter context menu to have a second option... something like

  • [T]o property with backing field
  • To [M]vx property with backing field
Was it helpful?

Solution

This is possible, even without creating any custom plugins or patterns, by using ReSharper Annotations. I have recently recorded a webinar with JetBrains, demonstrating exactly how to solve this with annotations. You can watch it here.

Short answer: the method

public void RaisePropertyChanged<T>(Expression<Func<T>> property)

of MvxNotifyPropertyChange.cs needs to be annotated with the NotifyPropertyChangedInvocatorAttribute, and then you could simply Alt-Enter on the property, and change it to a Property with change notificaton.

Now, since you can't (or don't want to) modify the source code of MvvmCross, you could apply those annotations externally, via XML. Take a look at the ExternalAnnotations directory, located in your ReSharper installation directory. It contains a bunch of external annotations for other MVVM frameworks. It's a simple XMLDoc format, so you could create an XML for MvvmCross and write the appropriate methods there. After that, save the file under a directory MvvmCross (or however the assembly is called), reload your solution, and it should just work!

For more information, please watch my webinar (link above), or JetBrains help

Here's an External Annotations file that will work with that method.

Cirrious.MvvmCross.ExternalAnnotations.xml

<?xml version="1.0" encoding="utf-8"?>
<assembly name="Cirrious.MvvmCross">

  <member name="M:Cirrious.MvvmCross.ViewModels.MvxNotifyPropertyChanged.RaisePropertyChanged``1(System.Linq.Expressions.Expression{System.Func{``0}})">
    <attribute ctor="M:JetBrains.Annotations.NotifyPropertyChangedInvocatorAttribute.#ctor" />
  </member>

</assembly>

And here it is in action:

Woot!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top