dotnet / dotnet/maui

ContentView inherits from the wrong base Layout class

Open
#7,613 20 comments 1 reaction 0 assignees View on GitHub
area-layout migration-compatibility partner platform/windows s/triaged s/verified t/bug
Dominant language
C#
Stars
23.3k
Forks
2k
Avg merge
1d 15h
Merged PRs (30d)
290

Description

### Description

There are two parallel layout implementations in .NET MAUI:
1. The new layout implementation with the base class of `Microsoft.Maui.Controls.Layout`
2. The legacy layout implementation from Xamarin.Forms with the base class of `Microsoft.Maui.Controls.Compatibility.Layout`

The new layout implementation is compatible with the new MAUI handlers. The old layout implementation from Xamarin.Forms is not compatible with the handlers and should be avoided.

The problem with the `ContentView` is that it has a handler, but is still inherits from the legacy layout `Microsoft.Maui.Controls.Compatibility.Layout`. This is a major source of bugs, as the usual layout logic might not be executed correctly or might not get executed at all on some platforms.

### Steps to Reproduce

Download and run the attached sample project on Windows.

[TestApp.zip](https://github.com/dotnet/maui/files/8799692/TestApp.zip)

The sample project contains a single `CustomView` inheriting from `ContentView`. The sole purpose of that `CustomView` is to override all the virtual `Measure`, `Arrange`, `Layout` etc. methods of the base class and dump some debug output when they are called. The sample applications has two additional buttons to call the `InvalidateMeasure` method on the `CustomView` from the base `VisualElement` class and from the `IView` interface.

Observe the debug output of the application. You can also put breakpoints in the overridden methods to be sure. Notice that nothing happens: nothing is called at all. All the layout logic of the `CustomView` is completely discarded.

### Version with bug

6.0 (current)

### Last version that worked well

Unknown/Other

### Affected platforms

Windows

### Affected platform versions

N/A

### Did you find any workaround?

Unless Microsoft changes the implementation of the `ContentView` to inherit from the new `Microsoft.Maui.Controls.Layut` and implements the necessary glue code, I cannot suggest any viable workaround, except not using `ContentView` or any templated controls in the .NET MAUI applications.

### Relevant log output

_No response_

Contributor guide

Open the contributing guide

Research direction

Review the ContentView inheritance and compare the new Microsoft.Maui.Controls.Layout with the legacy Microsoft.Maui.Controls.Compatibility.Layout. Start by running the attached TestApp.zip sample on Windows and observing the CustomView Measure, Arrange, Layout, and InvalidateMeasure behavior. Done means ContentView uses the handler-compatible layout path and the sample's overridden layout methods are invoked as expected.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
frontend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.