如何在 IOS 中使列表项达到其内容的完整高度? (在IOS中使用网格“*”行似乎被破坏了)

问题描述 投票:0回答:1

我在适用于 IOS 的 .net MAUI 中有一个列表项未按预期工作。问题似乎在于,当网格行设置为网格行高度“*”时,集合视图中每个单独项目中的网格视觉元素不会扩展到列表项内容的完整高度。通过布尔值使其可见后。下面的代码演示了我遇到的问题:

这是 xaml 代码请注意,我如何尝试为父元素提供从集合视图开始的固定高度,该高度需要扩展到父集合视图的 10000 单位高度的高度:

    <ContentPage.Content>
<Grid RowDefinitions="10000" BackgroundColor="BlanchedAlmond">
....
    <VerticalStackLayout Grid.Row="0" Grid.Column="0" BackgroundColor="Aquamarine">
        <VerticalStackLayout IsVisible="{Binding ShowBusyOverlay, Converter={StaticResource not}}" HeightRequest="10000">
<CollectionView x:Name="sessionSummaryList" ItemsSource="{Binding DataSections}" HeightRequest="10000">
    <CollectionView.ItemTemplate>
        <DataTemplate x:DataType="datacollection:DataSection">
            <Grid>
                <!--Here is where the issue lies with the grid star "*" height in the collection view list item-->
                <Grid.RowDefinitions>
                    <RowDefinition Height="40"/>
                    <RowDefinition Height="10"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <HorizontalStackLayout Margin="10, 5" Grid.Row="0" Grid.Column="0">
                    <HorizontalStackLayout.GestureRecognizers>
                        <TapGestureRecognizer Command="{Binding BindingContext.SectionTapped, Source={x:Reference Form} }" CommandParameter="{Binding .}" />
                    </HorizontalStackLayout.GestureRecognizers>
                    <Label Text="{Binding Title}" Style="{StaticResource label}" MinimumHeightRequest="40" VerticalTextAlignment="Center" TextColor="{StaticResource normalText}" FontAttributes="Bold" />
                    <Image  Source="{Binding Expanded, Converter={StaticResource expandedImageConverter}}" HorizontalOptions="End" HeightRequest="20" WidthRequest="20" Aspect="AspectFit" />
                </HorizontalStackLayout>
                <BoxView Grid.Row="1" Grid.Column="0" Color="#b9b9b9"/>
                <!--This is where the expansion boolean is that I mentioned earlier "{Binding Expanded}"-->
                <CollectionView Grid.Row="2" Grid.Column="0" IsVisible="{Binding Expanded}" ItemsSource="{Binding SectionItems}" ItemTemplate="{StaticResource SectionSelector}" EmptyView="No items to display" BackgroundColor="White"/>
            </Grid>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>
....
</VerticalStackLayout>
....
</VerticalStackLayout>
....
</Grid>
.......
</ContentPage>

这是“部分选择器”的当前模板

这是与后面代码中前面提到的 Binding“Expanded”布尔值相关联的 C# 视图模型扩展方法:

public ICommand SectionTapped => new Command<SessionSummarySection>(OnSectionTapped);
//......
private void OnSectionTapped(SessionSummarySection sessionSummarySection)
{
    sessionSummarySection.Expanded = !sessionSummarySection.Expanded;

}

我还附上了预期行为的图片,请参阅该图片以了解有关此代码应如何工作的更多详细信息:

在此输入图片描述

如您所见,我尝试通过将父 UI 元素设置为固定高度来使布局正常运行,以便网格可以根据集合视图的父级填充和扩展集合视图中的每个项目,但没有有用。

我能想到的使其工作的唯一其他方法是通过使用硬值而不是“6*”或“*”之类的值手动设置每个集合视图元素中每个网格的高度,但这意味着我会必须通过使用 x:Name 引用或类似的东西从每个网格中获取实际的视觉元素树来添加每个列表项内容的高度(我不知道该怎么做)。如果有人知道另一种方法或可以请告诉我如何让它发挥作用,那就太好了。

旁注,如果我让页面通过热重载重新渲染,它会因为某种原因突然工作,这意味着这是一个 MAUI 特定的错误(我认为)。另外需要注意的是,这个解决方案需要跨平台,不仅需要IOS,还需要Android。再说一遍,任何有关如何解决此问题的建议确实会非常有帮助。另外,如果您不介意明确让我知道该解决方案是否可以免费用于商业项目,请告诉我,以便我可以相应地归因于您。谢谢! (如果没有,为了安全起见,我会添加自己的想法。)

ios .net maui
1个回答
0
投票

尝试这个解决方法

如果不起作用,请尝试将宽度或高度设置为-1(自动)尝试在调试时切换值[iOS = -1,iOS = 1,iOS = -1],如果它按预期工作,则计划执行CollectionView 填充数据后。

WidthRequest="{OnPlatform iOS=-1,Default=1}"
HeightRequest="{OnPlatform iOS=-1,Default=1}"



<DataTemplate x:DataType="datacollection:DataSection">
     <Grid>
            <!--Here is where the issue lies with the grid star "*" height in the collection view list item-->
            <Grid.RowDefinitions>
                <RowDefinition Height="40"/>
                <RowDefinition Height="10"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
               
                 ...
                 ...
                 
        <CollectionView WidthRequest="{OnPlatform iOS=-1,Default=1}" 
                        HeightRequest="{OnPlatform iOS=-1,Default=1}"

                        Grid.Row="2" Grid.Column="0" 
                        IsVisible="{Binding Expanded}" 
                        ItemsSource="{Binding SectionItems}" 
                        ItemTemplate="{StaticResource SectionSelector}" 
                        EmptyView="No items to display" 
                        BackgroundColor="White"/>
    </Grid>
</DataTemplate>
© www.soinside.com 2019 - 2024. All rights reserved.