Get rid of "private field is assigned but its value is never used" warnings

Can you change it, so that when you generate the partial class from the XAML file, you insert

pragma warning disable 414

at the top?

It fires incorrectly for controls that are connected using MVVM (so no C# code ever refers to them), but I'd rather not turn off the warning completely because I want to know when it happens in my code.



  • BettyCrokkerBettyCrokker USMember ✭✭

    Ugh. Markup.

    That's supposed to say

    #pragma warning disable 414
  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭


    If no C# refers to it, then its not a wrong message.
    XAML doesn't reference fields, only properties - and they have to be public. So a message of private field in no way has anything to do with your XAML.

    Let's see your code for the property and the XAML that is using it.

  • BettyCrokkerBettyCrokker USMember ✭✭
    edited February 2017



    < Image x:Name="BatteryIcon" Source="{Binding BatteryIcon}" HorizontalOptions="Start" VerticalOptions="Center" HeightRequest="18" />

    [System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Forms.Build.Tasks.XamlG", "")] private global::Xamarin.Forms.Image BatteryIcon;

    Compiler says:
    xaml.g.cs(45,45): Warning CS0414: The private field BatteryIcon is assigned but its value is never used (CS0414)

    I define the control in XAML, the Forms generator creates a partial class for me, the control object is never referred to by name in any C# code, it connects using standard MVVM properties, but it's completely valid and doesn't need the warning.

Sign In or Register to comment.