unoplatform / uno

Open-source platform for building cross-platform native Mobile, Web, Desktop and Embedded apps quickly. Create rich, C#/XAML, single-codebase apps from any IDE. Hot Reload included! 90m+ NuGet Downloads!!
https://platform.uno
Apache License 2.0
8.89k stars 720 forks source link

[iOS] The size measured by the Measure method is wrong #3657

Open Daoting opened 4 years ago

Daoting commented 4 years ago

Current behavior

All custom controls have this error on iOS with Uno V3.0. For example:

    <Style TargetType="a:Menu">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="a:Menu">
                    <menu:MenuPanel x:Name="Panel" Background="Transparent" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

MenuPanel inherited from Panel. Used to place all MenuItem.

    <Style TargetType="a:MenuItem">
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="a:MenuItem">
                    <Grid x:Name="RootElement" Background="{TemplateBinding Background}">
                        <Rectangle x:Name="Rect" IsHitTestVisible="False"/>
                        <Grid x:Name="ContentGrid" Margin="10">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <TextBlock x:Name="ElementIcon"
                                       Text="{Binding Icon, Converter={StaticResource CvIconToChar}, RelativeSource={RelativeSource TemplatedParent}}"
                                       FontFamily="{StaticResource IconFont}"
                                       FontSize="20"
                                       Margin="0,0,4,0"
                                       VerticalAlignment="Center" />
                            <TextBlock x:Name="ElementText"
                                       Text="{TemplateBinding ID}"
                                       VerticalAlignment="Center"
                                       Grid.Column="1" />
                            <TextBlock x:Name="ElementSub"
                                       Text="&#xE086;"
                                       FontFamily="{StaticResource IconFont}"
                                       Visibility="Collapsed"
                                       VerticalAlignment="Center"
                                       Grid.Column="2" />
                        </Grid>

                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

MenuPanel measure items.

        Size MeasureMenu()
        {
            double width = 0;
            double height = 0;
            foreach (var mi in Children.OfType<MenuItem>())
            {
                mi.Measure(_availableSize);
                if (mi.Visibility == Visibility.Visible)
                {
                    width += mi.DesiredSize.Width;
                    if (mi.DesiredSize.Height > height)
                        height = mi.DesiredSize.Height;
                }
            }
            return new Size(width, height);
        }

mi.DesiredSize is always same as _availableSize . Other custom controls also have this error. I can't figure out the reason, the impact is relatively large. Thanks.

Expected behavior

How to reproduce it (as minimally and precisely as possible)

Environment

Nuget Package:

Package Version(s):

Affected platform(s):

Visual Studio:

Relevant plugins:

Anything else we need to know?

jeromelaban commented 4 years ago

Thanks for the report. What's the effect of this behavior ? Can you provide a screenshot of the error ?

Daoting commented 4 years ago

These custom controls have a common feature, that is, the parent control creates child controls and then adds them to the internal Panel. No error message, just the measurement result is wrong. Take the menu as an example. Menu MenuPanel load items MenuPanel MeasureOverride

Debug image

Screenshot iOS 2020-07-30_08-40-59-上午

Android Screenshot_1596068182

dr1rrb commented 3 years ago

Sounds related to https://github.com/unoplatform/uno/issues/1554

Youssef1313 commented 1 year ago

This issue needs to be re-verified once https://github.com/unoplatform/uno/pull/13839 is merged