Skip to content

Overview

Detect errors, improvements and maintainability suggestions with code analysis.

Introduction

MFractor includes a code-analysis engine that inspects your XAML code for a wide range of common issues.

Managing Analysers

MFractors code analysers can be enabled or disabled at the application-level through the product Preferences.

To enable or disable a particular code analyser, first, navigate to the top menu bar, find the MFractor tab and then click Preferences.

Scroll down to Code Analysis and you can search for a specific one or toggle to disable at an application level.

If you'd like to disable at for only a particular document, please see our Disabling Analysers article.

MFractor currently includes 95 analysers.

Available Code Analysers

Diagnostic ID Name Description
Duplicate AutomationIds Inspects a Xaml document for occurances of duplicate AutomationId declarations.
Empty AutomationId Declaration Inspects a Xaml document for occurances of duplicate AutomationId declarations.
x:Name Has Invalid Characters Checks that x:Name expressions define a valid code-behind variable name. A value declared by an x:Name attribute must start with a @, _ or a-Z character followed by underscores or alpha-numeric characters.
Duplicate Code Behind Field Declarations Checks that the value assigned to an x:Name attribute is unique within the scope of the document.
Empty Code Behind Field Declaration Inspects occurances of the x:Name attribute and validates that a value is assigned; empty x:Name expressions generate an empty named code-behind field, causing compilation errors.
ContentPage Has Multiple Direct Children Inspects usages of ContentPage and checks that it only has a single child view. Assigning multiple children into a ContentPage is a common mistake where the developer usually intended to wrap the chid views with a Grid or a StackLayout.
ContentView Has Multiple Direct Children Inspects usages of the ContentView element and checks that it only has a single child view. Assigning multiple children into a ContentView is a common mistake where the developer usually intended to wrap the chid views with a Grid or a StackLayout.
Validate Binding Expressions Return A Property Evaluates a Binding expression and validates that it points to property within the binding context. This analyser requires an explict or implicit binding context.
Binding Against Non-Public Property Inspects data-binding expressions and validates that the property return is a public property. Data-binding against a non-public property causes data-binding to fail.
Binding Expressions Resolve Inspects Binding expressions and validates that the symbol referred to in the binding context exists. This analyser requires an explict or implicit binding context.
Binding Expression Return Type Mismatch Validates that the .NET symbol returned by a binding expression matches the expected type for the property.
No Key Provided To DynamicResource Expression Inspects DynamicResource expressions and validates that a resource key has been provided.
Empty Event Handler Assignment Checks that an event handler assignment is not empty as empty event handler assignments will cause a compilation error.
Event Handler Exists In Code Behind Class Checks that an event callback referenced referenced by an attribute value exists in the code behind class.
Event Handler Signature Mismatch Checks that the signature for the event callback in a code behind class matches the expected signature for the property it is assigned to.
Referenced Code Behind Field Exists Inspects usages of the x:Reference expression and validates the referenced element has been declared within the document. x:Reference expressions are used to resolve another Xaml node has a code behind field defined using the x:Name attribute.
Value Converter Input Type Mismatch Inspects the Converter property of a Bindingexpression and validates that the input type is correct. This analyser requires that the IValueConverter implementation uses the ValueConversion attribute to declare it input type.
Value Converter Output Type Mismatch Inspects the Converter component of a Binding expression and validates that the returned output type is valid for the parent attribute. This analyser requires that the IValueConverter implementation uses the ValueConversion attribute to declare it input type.
Unresolved .NET Symbols Within Xaml Expression Inspects a .net symbol reference (eg local:MyClass.MyProperty) and validates that the symbol portion ('MyClass.MyProperty') resolves to a .NET symbol within the current project and it's references.
Unresolved Namespace Within Xaml Expression Inspects a .net symbol reference (eg local:MyClass.MyProperty) and validates that the namespace portion ('local') resolves to a xmlns declaration in the current document.
Invalid Named Font Size Inspects FontSize attributes and validates that the named size provided to it is a valid font size name (Micro, Small, Medium, Large).
Generic Usage Is Missing x:TypeArguments Inspects generic classes that are instantiated through Xaml and validates that an x:TypeArguments attribute or property assignment node is present.
x:TypeArguments Used On Non-Generic Class Inspects for usages of x:TypeArguments on elements that are non-generic classes.
Unknown Type Provided To Generic Inspects usages of x:TypeArguments and validates that the type provided exists.
Column Is Outside Grid Boundaries This code analyser inspects usages of the Grid.Column attribute and validates that the column provided is within the total columns declared by the parent grid.
ColumnSpan Is Outside Grid Boundaries This code analyser inspects usages of the Grid.ColumnSpan attribute and validates that the span provided is within the total columns declared by the parent grid.
ColumnSpan Is Zero This code analyser inspects usages of the Grid.ColumnSpan attribute and validates that the span provided is a non-zero value.
Grid Row/Column Setter Is Not A Number This code analyser inspects usages of the Grid.Column attribute and validates that the element is inside a Grid.
Grid.Column Usage Is Redundant This code analyser inspects usages of the Grid.Column attribute and validates that the element is inside a Grid.
Grid.ColumnSpan Usage Is Redundant This code analyser inspects usages of the Grid.ColumnSpan attribute and validates that the element is inside a Grid.
Grid.Row Usage Is Redundant This code analyser inspects usages of the Grid.Row attribute and validates that the element is inside a Grid.
Grid.RowSpan Usage Is Redundant This code analyser inspects usages of the Grid.RowSpan attribute and validates that the element is inside a Grid.
Row Is Outside Grid Boundaries This code analyser inspects usages of the Grid.Row attribute and validates that the row provided is within the total rows declared by the parent grid.
RowSpan Is Outside Grid Boundaries This code analyser inspects usages of the Grid.RowSpan attribute and validates that the span provided is within the total rows declared by the parent grid.
RowSpan Is Zero This code analyser inspects usages of the Grid.RowSpan attribute and validates that the span provided is a non-zero value.
Detect Missing Image In Linked Projects This code analyser inspects the value provided into an ImageSource and validates that an image of that name exists within any iOS or Android projects that reference this shared project or PCL.
OnIdiom Return Type Mismatch Checks the type returned by a Xamarin.Forms.OnIdiom element is valid with the parent property type.
OnPlatform Return Type Mismatch Checks the type returned by a Xamarin.Forms.OnPlatform element is valid with the parent property type.
Referenced Attribute Member Exists In Parent Type Checks that an attribute resolves to a member within its parent type.
Property Node Maps To Member In Parent Type Checks that a property node resolves to a member within its parent type.
Property Setter Node Misuse Inspects for property setters that don't apply to the outer class. For example, if a developer used OnIdiom.Phone inside a OnPlatform element, the OnIdiom.Phone property setter makes no sense within the given context.
Property Setter Type Mismatch When using MyClass.MyProperty node setter syntax, validate that the inner child node returns a .NET object of the correct type for the property.
Duplicate Resource Dictionary Keys Validates the each resource entry within a resource dictionary has a unique key.
Resource Entry Does Not Define Key Validates the elements provided to a resource dictionary supply an x:Key attribute to declare their resource dictionary key.
Resource Key Conflict Inspects resource key declarations and validates that the resource key is not already defined in other files that are used by this file.
Unused Resource Inspects that resource dictionary entries are used via the StaticResource markup extension expression within this document.
Missing Microsoft Schema Inspects at the root xaml node and verifies that it references the Microsoft Xaml schema: http://schemas.microsoft.com/winfx/2009/xaml. This schema is required for Xamarin Forms Xaml documents.
Duplicate ShellItem Route Inspects Xamarin.Forms.ShellItem elements and validates that their Route is unique.
Validate Setter Attached Property Usage Inspects the Property attribute for a Xamarin.Forms.Setter and, when it is referencing an attached property, validates that the namespace, class and attached property exist.
Setter Property Exists Inspects the Property attribute for a Xamarin.Forms.Setter and validates that it exists in the parents specified TargetType.
Empty Shell Route Scheme Providing an empty value into the RouteScheme property of a Xamarin.Forms.Shell element will cause the application to crash. This code inspection validates that a value is provided to the RouteScheme property.
Multiple VisualElements Defined In Shell Content Inspects usages of the Xamarin.Forms.ShellContent and validates that only one page or view is declared within it.
Shell Requires At Least One Item When using Xamarin.Forms.Shell you must provide at least one item declaration.
Ambiguous Static Resource Reference Inspects StaticResource expressions and checks if one or more static resources will be returned from the resource expression.
No Key Provided To Static Resource Expression Inspects StaticResource expressions and validates that a resource key has been provided.
StaticResource Return Type Mismatch Validates that the symbol returned by a StaticResource expression matches the expected type for the property.
x:Static Return Type Mismatch Validates that the .NET symbol returned by an x:Static expressions matches the expected type for the property.
Undefined Static Resource Usage Validates that the element referenced by a StaticResource expression resource lookup resolves to a resource defined in the xaml file.
Style Is Missing TargetType When a Style is used within XAML, it should always specify a type it targets using the TargetType property. This analysis check inspects for usages of Style that don't assign the TargetType property.
Style Or Trigger Has Duplicate Setters Inspects Style and Trigger declarations and checks if there is are multiple Setter's for a property.
Property Setter Does Not Exist In Style TargetType This code inspection looks at Setter elements when they are used inside a Style and validates that the member specified in the Property attribute exists on the type symbol referenced in the parent Styles TargetType attribute.
Style TargetType Does Not Match BasedOn TargetType Inspects XAML styles that use the BasedOn property to inherit from another style and validates the TargetType of the current style matches the TargetType defined by the BasedOn style.
Invalid Thickness Attribute Value Inspects attribute properties that use the Xamarin.Forms.Thickness type and checks the value used can be translated to a thickness. For example, Thickness could accidentally be provided 0,5,05, with the intention of it being 0,5,0,5; the first example has three arguments while the second has four. This misuse would cause the app to crash when using inflated XAML or for XAMLC to fail.
Trigger Is Missing Target Type Inspects XAML elements that derive from Xamarin.Forms.TriggerBase and validates that they include a TargetType attribute
Trigger TargetType Does Not Match Parent Inspects usages of the TargetType property for a Xamarin.Forms.TriggerBase and validates that the type provided matches the outer XAML node.
Duplicate Namespace Declaration Checks that an xml namespace points to a unique namespace and assembly. For example if both xmlns:local="clr-namespace:MFractor.Licensing.MobileApp" and xmlns:myassembly="clr-namespace:MFractor.Licensing.MobileApp" were declared, this analyser would warn that they both reference the same assembly and namespace.
Unresolved Xml Namespace Checks that the namespace used on the xml nodes is defined within the current document.
Unresolved Xmlns Assembly Checks that the 'assembly' component of an xmlns statement resolves to an assembly referenced by the project.
Class Does Not Have Attached Property Looks for attached properties (for example Grid.Row) and validates they exist in the class that they are attempting to use.
Empty Value Assignment Detects when a boolean, double, long or integer value is being assigned an empty value and will cause a compilation error.
Obsolete Property Used Checks for attributes that are marked as obsolete/deprecated.
Unknown Static Property Value For attributes that accept a class object, inspects that the literal value maps to a static property or field in the class type. For example, the LayoutOptions class has the static fields Fill or CentreAndExpand that can be used a literal attribute values. If CentreAndFill was provided (an invalid value), this analyser would inspect the LayoutOptions class for a static field named CentreAndFill and trigger an analysis error when it couldn't be found.
Validate Value Types Inspects the input provided to attributes that expect value types (int, bool, double etc) and validates that the input is valid.
Xaml Node Resolves Checks that xaml nodes map to a valid .NET symbol.
Hexadecimal Value Matches Named Color Inspects HexaDecimal color values and matches them against the named color constants.
Static Resource Style Target Type Is Incompatible When a static resource expression returns a style, this code inspection verifies that the TargetType of the given style is compatible with
Thickness Value Can Be Simplified Inspect's thickness attribute values and verifies if the values can be simplified. For example, a thickness value of 20,0,20,0 could be simplified to 20,0.
Color Value Matches Static Resource When assigning a color a hexadecimal or named constant, this code inspection detects if that color value matches an available color resource.
Thickness Value Can Be Replaced By Static Resource Inspects assignments of Xamarin.Forms.Thickness values in XAML and matches them to declared static resources.
Property Value Is Already Applied By Style When a Style is applied within XAML, it should always specify a type it targets using the TargetType property. This analysis check inspects for usages of Style that don't assign the TargetType property.
Element Can Use Available Style Inspects XAML elements that do not have a style applied and, if possible, matches them to an available style that targets the element type and also applies the same properties.
Malformed Hexadecimal Color Value Inspects hexadecimal color values and validates that they are in a format supported by Xamarin.Forms.
Unknown Field Modifier Inspects occurances of the x:FieldModifer attribute and validates that the value is one of the following keywords.<r>
  • private: Specifies that the generated field for the XAML element is accessible only within the body of the class in which it is declared.
  • public: Specifies that the generated field for the XAML element has no access restrictions.
  • protected: Specifies that the generated field for the XAML element is accessible within its class and by derived class instances.
  • internal: Specifies that the generated field for the XAML element is accessible only within types in the same assembly.
  • notpublic: Specifies that the generated field for the XAML element is accessible only within types in the same assembly.
Color Value Closely Matches Available Static Resource Inspects color values and checks if they closely match the color value defined by a static resource.
Slider Minimum Set Before Maximum Inspects slider elements and verifies that the user sets the maximum before the minimum
Cell Usage Within CollectionView ItemTemplate Inspects the DataTemplate within a CollectionViews ItemTemplate property and verify that it does not use a cell.
Glyph Does Not Exist In Font When a font is applied to a XAML element via the FontFamily attribute, this analyser inspects the character code provided and verifies that it exists in the referenced font asset.
Thickness Value Can Be Consolidated Inspects thickness values and verifies if there are other thickness in the projects that also declare
RefreshView Content Should Be Scrollable Layout Inspects the inner element of a refresh view and verifies that it derives from either a ListView, ScrollView or CollectionView.
Color Value Can Be Consolidated Inspects color values and verifies if there are other colors in the projects that also declare that specific color.
Unknown Embedded Font Reference Inspects FontFamily attributes and validates that the referenced embedded font asset is defined.
Unescaped Newline In String Literal When a newline character, \n, is used within a string literal in XAML, it will not render as expected. Newlines should be added to XAML using the escaped character code.
Unknown OnPlatform Value Checks the Platform value provided to a Xamarin.Forms.On element and verifies that it is a known platform within the Xamarin.Forms.Device class.
Data Template Expected Inspects property nodes that accept a data template and verifies that the content is a DataTemplate.
ScrollView Has Multiple Direct Children Inspects usages of ScrollView and checks that it only has a single child view. Assigning multiple children to a ScrollView is a common mistake where the developer usually intended to wrap the chid views with a Grid or a StackLayout.

Comments