Skip to content

VisualElement.HeightRequest defaults to 0 instead of -1 when using OnIdiom default value #9978

Description

@ZackAllen

Description

VisualElement.WidthRequest and VisualElement.HeightRequest have a default value of 0 if an OnIdiom condition is not set. This results in an element not being visible instead of being -1 which doesn't change behavior.

Steps to Reproduce

  1. Set a height request to "{OnIdiom Phone=200}".
  2. Run on desktop.
  3. Observe that the visual element has 0 height, instead of the default height it would have with no height request set.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage x:Class="MauiWidthBugApp.MainPage"
             xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">

    <ScrollView>
        <VerticalStackLayout Padding="30,0"
                             Spacing="25"
                             VerticalOptions="Center"
                             WidthRequest="300">

            <Label FontSize="32"
                   HorizontalOptions="Center"
                   SemanticProperties.HeadingLevel="Level1"
                   Text="Working image:" />
            <!--  No height request, image stretches to fit  -->
            <Image HorizontalOptions="Center"
                   SemanticProperties.Description="Working image"
                   Source="dotnet_bot.png" />

            <Label FontSize="32"
                   HorizontalOptions="Center"
                   SemanticProperties.HeadingLevel="Level1"
                   Text="Image with bug:" />
            <!--  With no default set, platforms that aren't Phone have this value set to 0, making the visual element disappear instead of stretching to fit the parent like it should  -->
            <Image HeightRequest="{OnIdiom Phone=200}"
                   HorizontalOptions="Center"
                   SemanticProperties.Description="Defaults to height request 0 on desktop"
                   Source="dotnet_bot.png" />

        </VerticalStackLayout>
    </ScrollView>

</ContentPage>

Link to public reproduction project repository

https://github.com/ZackAllen/mauiHeightRequestBug

Version with bug

6.0.400

Last version that worked well

Unknown/Other

Affected platforms

iOS, Android, Windows

Affected platform versions

All

Did you find any workaround?

Manually setting a default of -1.

Relevant log output

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-xamlXAML, CSS, Triggers, Behaviorsplatform/androidplatform/iosplatform/windowss/triagedIssue has been revieweds/try-latest-versionPlease try to reproduce the potential issue on the latest public versions/verifiedVerified / Reproducible Issue ready for Engineering Triaget/bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions