Dan Bryant
Dan Bryant

Reputation: 27505

ReSharper warnings with MVVM

As I implement the MVVM pattern with WPF, I'm finding that ReSharper is often warning me that certain properties are never used in my ViewModels. The problem is that they are being used, but only by the data binding system. Has anyone else encountered this annoyance and is there a way to help ReSharper realize that these properties are, indeed, being used? I am glad, at least, that VS 2010 properly realizes that [Import] tagged members won't "always be null", but hopefully I can fix this issue as well.

Upvotes: 31

Views: 7716

Answers (6)

Maxx Daymon
Maxx Daymon

Reputation: 488

Are the properties public or internal? In my experience, ReSharper doesn't warn on public (since there's no way it could tell that the members aren't being used externally) but it will warn on internal members since they can only be used within that assembly (InternalsVisibleTo notwithstanding).

Upvotes: 0

controlflow
controlflow

Reputation: 6737

This is because of the weakly-typed nature of XAML bindings.

To make ReSharper able to resolve what properties of VM you use from XAML view, you need to introduce data context type annotations for {Binding}s in markup. See the "Binding assistance" section in this blog post for details. You will get correct usage analysis, navigation and refactorings support when ReSharper will known data context type.

ReSharper also knows about OneWay/OneWayToSource/TwoWay bindings modes and marks properties getters/setters/both accessors as used respectively.

Upvotes: 2

Ben Hoffstein
Ben Hoffstein

Reputation: 103345

A crude workaround would be to disable the warning altogether:

Under ReSharper > Options > Code Inspection > Inspection Severity, set the warning level for this item to "Do not show".

This is obviously not ideal, but it depends on your level of annoyance with the false positives.

Upvotes: 4

user203570
user203570

Reputation:

You can try two different options.

  • Option 1: Reduce the severity of the ReSharper inspection to "Hint".

  • Option 2: Use the "Suppress inspection with comment" item ReSharper provides for the properties that generate the warning that you know are being used.

Personally, I'd go with reducing the severity to "Hint".

Upvotes: 6

wal
wal

Reputation: 17729

You can use External Annotations to indicate to ReSharper the method is used and thus not to warn you. See the ReSharper docs on that here.

You need to decorate any such methods with [UsedImplicitlyAttribute].

Before using the attribute, you see:

enter image description here

and then, after applying the attribute:

[UsedImplicitly(ImplicitUseTargetFlags.WithMembers)]
class NotUsed
{
    public int Field1 { get; set; }
    public int Field2 { get; set; }
}

Upvotes: 32

Adrian Botor
Adrian Botor

Reputation: 372

Use

<UserControl
...
xmlns:vm="clr-namespace:YourProject.ViewModels" mc:Ignorable="d"
d:DataContext="{d:DesignInstance vm:SomeClassViewModel}">

It 'stick's View to Model. In View you could see model properties and vice versa - in model properties should be used.

Upvotes: 16

Related Questions