Inefficient Layout Warnings

The C# compiler gives you a lot of hints about dead code — that is, code that can never have any impact on your program at runtime. Luckily, the compiler is good at optimizing that away so the performance of your application is not impacted. However leaving that code in place clutters up your code, making it more difficult to understand and maintain.

You can have a similar situation with Xaml files — attributes and elements that are either completely unnecessary or just more complicated than needed. Unfortunately, the IDE doesn’t warn you about these, and adding unnecessary attributes and layouts can make the code harder to read and slow down the performance of your app.

The performance impact of unnecessary Xaml elements using a sample app we developed to demonstrate the issue are described in the following post: https://criticalhittech.com/2019/04/01/inefficient-xamarin-forms-layout-test/

These warnings target readability and performance of Xaml, not the correctness. In cases where you want to focus on fixing correctness issues first, you may disable these warnings; see XamRight Settings.

[XR5301, XR5302] Redundant Layout Options Declarations on children of StackLayout

HorizontalOptions and VerticalOptions are used for positioning child views within a parent StackLayout. However, not all possible values of these options are relevant in all cases, depending on the orientation of the StackLayout. These two rules ensure the values are appropriately set:

  • Values Start, Center, End and Fill only apply to Layout Options opposite the direction of the containing StackLayout. (eg. XamRight warns if HorizontalOptions is set to Start on a child of a Horizontal orientation StackLayout). Unless the “AndExpand” suffix is applied, setting this will have no impact on the layout.
  • The “AndExpand” suffix is only relevant when applied to Layout Options in the direction of the StackLayout. (eg. XamRight warns if HorizontalOptions is set to StartAndExpand on a child of a Vertical StackLayout). In other words, StartAndExpand is identical to Start in terms of layout.

[XR5303] Unnecessary StackLayout wrapping

Wrapping views with StackLayouts can slow down performance due the complexity of computing the size of each StackLayout. XamRight warns if a StackLayout is used to wrap a single child element where it is likely that you could easily achieve the same effect without the extra StackLayout.

[XR5309] Unnecessary usage of Grid properties on views that are not contained in a Grid

Grid.Row, Grid.Column, Grid.RowSpan and Grid.ColumnSpan are attached properties used to position direct child views of a Grid. Hence, these properties don’t serve a purpose when applied on views that are not a direct child of a Grid. XamRight warns if these unnecessary usages are found

[XR5310] Unnecessary “AndExpand” suffix for Layout Options on child views of Grids

Because a Grid’s RowDefinitions and ColumnDefinitions define the space allocated to each child view, the “AndExpand” suffix is unnecessary and does not expand the space allocated to the child view

[XR5311, XR5312] Unnecessary usage of Layout Options on child views of RelativeLayouts and AbsoluteLayouts

HorizontalOptions and VerticalOptions property values are ignored when applied to child views of RelativeLayouts and AbsoluteLayouts as these wrapper layouts have their own attached properties that function to position their child views. XamRight warns if it detects these unnecessary usages of HorizontalOptions and VerticalOptions.

[XR5313, XR5314] Unnecessary usage of Grid properties on Grid’s child views

If a Grid does not have a set of Row or Column Definitions defined, a warning is emitted when setting the Grid.Row or Grid.Column to “0”, or Grid.ColumnSpan or Grid.RowSpan to “1” (respectively) on child views. These settings will not affect the layout at all, and are considered unnecessary without the corresponding Definition.

Author