-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Description
Description
Setting WidthRequest and/or HeightRequest on any Layout control like Grid or StackLayout in MAUI causes it to sometimes Measure slightly larger than the requested size on Android. Even setting MaximumWidthRequest and MaximumHeightRequest yields the same slightly oversized measure for some values. XF however always measures to the requested values.
While the oversized amount is always less than 1, that error can compound in various cases, especially with complex custom layouts containing many sub layouts which end up causing elements that were expected to fit in a given space to be forced into another location. For example, take a layout with a WidthRequest LWR that has N children that are grids with WidthRequest IWR such that LWR = K*(IWR+S)-S where S is some inter-item spacing and K is the number of elements that should fit in the given width LWR. In XF, K items will always fit, but in MAUI sometimes K will fit and sometimes only K-1 will fit due to the measure error.
Steps to Reproduce
- Create a new MAUI and XF application (android only)
- In App.cs: MainPage = new MainPage(); (aka delete the shell)
- Replace the content of MainPage.xaml with a Grid named "layout" and set the Grid WidthRequest = 117 and HeightRequest = 117
<ContentPage.Content>
<Grid
x:Name="layout"
WidthRequest="117"
HeightRequest="117">
</Grid>
</ContentPage.Content>
- Override MainPage.LayoutChildren and call/log layout.Measure(width, height);
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
protected override void LayoutChildren(double x, double y, double width, double height)
{
var sizeRequest = layout.Measure(width, height);
Console.WriteLine($"Layout measures to: {sizeRequest}");
base.LayoutChildren(x, y, width, height);
}
}
- Create a Pixel 7 Pro on api 34 via ADM from Visual Studio with default settings
- Deploy and run both apps
- Observe the same width/height yielding different measure results:
- MAUI: Request=117.142857142857x117.142857142857, Minimum=117.142857142857x117.142857142857
- XF: Request=117x117, Minimum=117x117
- Replace MainPage.xaml content with:
<ContentPage.Content>
<FlexLayout
x:Name="layout"
WidthRequest="234"
HeightRequest="234"
BackgroundColor="Yellow"
Wrap="Wrap">
<BoxView WidthRequest="117" HeightRequest="117" BackgroundColor="Red" />
<BoxView WidthRequest="117" HeightRequest="117" BackgroundColor="Black" />
</FlexLayout>
</ContentPage.Content>
- Observe the unexpected layout where the red and black squares are vertically stacked instead of horizontally due to the error pushing it down a row as explained in this comment:
Link to public reproduction project repository
No response
Version with bug
8.0.92 SR9.2
Is this a regression from previous behavior?
Yes, this used to work in Xamarin.Forms
Last version that worked well
No response
Affected platforms
Android
Affected platform versions
Android 34 and up on Pixel 7 Pro emulator
Did you find any workaround?
No response
Relevant log output
No response