开发者

ReSharper warnings with MVVM

开发者 https://www.devze.com 2022-12-29 07:06 出处:网络
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

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.


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:

ReSharper warnings with MVVM

and then, after applying the attribute:

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


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.


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".


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.


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消