Listview 如何自动调整ScrollView的大小以适应xamarin表单中的内容

Listview 如何自动调整ScrollView的大小以适应xamarin表单中的内容,listview,xamarin.forms,scrollview,Listview,Xamarin.forms,Scrollview,嗨,我想自动调整滚动视图的大小以适应其内容。在横向模式下的所有设备中,它应该滚动到列表视图的末尾。现在我的问题是,我必须根据设备不断更改滚动视图的高度,这是不正确的。这是我的密码 if (svMainBody.Children.Contains(grdListBody)) { svMainBody.Children.Remove(grdListBody); svMain = new ScrollView { Content = grdListBody }; svMainBody.Children

嗨,我想自动调整滚动视图的大小以适应其内容。在横向模式下的所有设备中,它应该滚动到列表视图的末尾。现在我的问题是,我必须根据设备不断更改滚动视图的高度,这是不正确的。这是我的密码

if (svMainBody.Children.Contains(grdListBody))
{
svMainBody.Children.Remove(grdListBody);
svMain = new ScrollView { Content = grdListBody };
svMainBody.Children.Add(svMain);
}
else
{
svMainBody.Children.Add(svMain);
}
rdListViewHeight.Height = height * 0.30;
}
}
else
{
if (IsPortrait(this))
{
//Fix for scrolling only listview in portrait
if (svMainBody.Children.Contains(svMain))
{
svMainBody.Children.Remove(svMain);
svMainBody.Children.Add(grdListBody);
}
else
{
svMainBody.Children.Add(grdListBody);
}

                            if (Settings.Flowsheet.sl1 == "")
                                Settings.Flowsheet.sl1 = sl1.Height.ToString();

                            dstk1Height = Settings.Flowsheet.sl1 == "" ? sl1.Height : Convert.ToDouble(Settings.Flowsheet.sl1);

                            if (Device.RuntimePlatform == Device.Android)
                                rdListViewHeight.Height = height - 160 - sl1.Height - 40;

                            else
                                rdListViewHeight.Height = height - 160 - dstk1Height - 30;
                        }
                        else
                        {
                            //Fix for scrolling entire page in landscape
                            if (svMainBody.Children.Contains(grdListBody))
                            {
                                svMainBody.Children.Remove(grdListBody);
                                svMain = new ScrollView { Content = grdListBody };
                                svMainBody.Children.Add(svMain);
                            }
                            else
                            {
                                svMainBody.Children.Add(svMain);
                            }
                            rdListViewHeight.Height = height * 3.0;
                        }
这是我的xaml代码

 <StackLayout  x:Name="svMainBody"  Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="13" VerticalOptions="FillAndExpand">

                    <Grid x:Name="grdListBody" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="13"  BackgroundColor="#EEEEEE"  VerticalOptions="FillAndExpand">
                        <Grid.RowSpacing>0</Grid.RowSpacing>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"></RowDefinition>
                            <RowDefinition x:Name="rdListViewHeight" Height="Auto"></RowDefinition>
                        </Grid.RowDefinitions>
                        <StackLayout Padding="10,10,10,10"  Grid.Row="0" x:Name="sl1" VerticalOptions="Start">
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="Auto"></RowDefinition>
                                    <RowDefinition Height="Auto"></RowDefinition>
                                    <RowDefinition Height="Auto"></RowDefinition>
                                    <RowDefinition Height="Auto"></RowDefinition>
                                    <RowDefinition Height="Auto"></RowDefinition>
                                    <RowDefinition Height="Auto"></RowDefinition>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"></ColumnDefinition>
                                    <ColumnDefinition Width="40"></ColumnDefinition>
                                    <ColumnDefinition Width="*"></ColumnDefinition>
                                </Grid.ColumnDefinitions>
                                <Label Grid.Row="0" Grid.Column="0" x:Name="lblSetTime" Text="{Binding SetTime}" TextColor="Black" HorizontalOptions="Start"></Label>
                                <Label Grid.Row="0" Grid.Column="1" x:Name="lblMeetingRoom" Grid.ColumnSpan="2" Text="{Binding MeetingRoom}" TextColor="Black" HorizontalOptions="Start"></Label>
                                <Label Grid.Row="1" Grid.Column="0" x:Name="lblSetByStrikeBy" Text="{Binding SetByStrikeBy}" TextColor="Black" HorizontalOptions="Start" IsVisible="{Binding IsVisibleLblSetByStrikeBy}"></Label>
                                <Label Grid.Row="1" Grid.Column="1" x:Name="lblRoomSetter" Grid.ColumnSpan="2" Text="{Binding RoomSetter}" TextColor="Black" HorizontalOptions="Start" IsVisible="{Binding IsVisibleLblRoomSetter}"></Label>
                                <Label Grid.Row="2" Grid.Column="0" x:Name="lblStartTime" Text="{Binding StartTime}" TextColor="Black" HorizontalOptions="Start"></Label>
                                <Image  Grid.Row="2" Grid.Column="1" x:Name="imgPromoterScore" Source="" HeightRequest="20" WidthRequest="20" HorizontalOptions="Start"  IsVisible="False" ></Image>
                                <Label Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" x:Name="lblClientCompany" Text="{Binding ClientCompany}" TextColor="Black" HorizontalOptions="Start"></Label>
                                <Label Grid.Row="3" Grid.Column="0" x:Name="lblEndTime" Text="{Binding EndTime}" TextColor="Black" HorizontalOptions="Start"></Label>
                                <Image  Grid.Row="3" Grid.Column="1" x:Name="imgPlannerScore" Source=""  HeightRequest="20" WidthRequest="20" HorizontalOptions="Start"  IsVisible="False"></Image>
                                <Label Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="2" x:Name="lblClientPlanner" Text="{Binding ClientPlanner}" TextColor="Black" HorizontalOptions="Start"></Label>
                                <Label Grid.Row="4" Grid.Column="0" x:Name="lblStrikeTime" Text="{Binding StrikeTime}" TextColor="Black" HorizontalOptions="Start"></Label>
                                <Label Grid.Row="4" Grid.Column="1" x:Name="lblPostAs" Grid.ColumnSpan="2" Text="{Binding PostAs}" TextColor="Black" HorizontalOptions="Start"></Label>
                                <Label Grid.Row="5" Grid.Column="0" x:Name="lblAction"  Text="{Binding Action}" TextColor="Black" HorizontalOptions="Start"></Label>
                                <Label Grid.Row="5" Grid.Column="1" x:Name="lblVenuePlanner_PSAVSalesPerson"  Grid.ColumnSpan="2" Text="{Binding VennuePlanner_PSAVSalesPerson}" TextColor="Black" HorizontalOptions="Start"></Label>
                            </Grid>
                        </StackLayout>

                        <StackLayout Grid.Row="1"  VerticalOptions="FillAndExpand" Orientation="Vertical">
                            <BoxView  BackgroundColor="Black" HeightRequest="1" HorizontalOptions="FillAndExpand"/>
                            <StackLayout Padding="10,5,10,5" Orientation="Vertical" BackgroundColor="#EEEEEE" x:Name="sl2"  VerticalOptions="FillAndExpand">
                                <local1:CustomListviewEventDetails
                                    x:Name="lstItems" HasUnevenRows="True" SeparatorVisibility="None" BackgroundColor="#EEEEEE" ItemsSource="{Binding lstViewItemSource}" ItemSelected="lstItems_ItemSelected" VerticalOptions="FillAndExpand"  
                                                       IsGroupingEnabled="True">
                                    <local1:CustomListviewEventDetails.GroupHeaderTemplate>
                                        <DataTemplate>
                                            <local1:CustomViewCellFS>
                                                <StackLayout HorizontalOptions="FillAndExpand" >
                                                    <!--Padding="20,0,0,0" VerticalOptions="CenterAndExpand"-->
                                                    <!--HorizontalOptions="CenterAndExpand"-->
                                                    <Label Text="{Binding SectionName}" FontSize="15"  FontAttributes="Bold" TextColor="White" VerticalOptions="CenterAndExpand" VerticalTextAlignment="Center" XAlign="Center" />
                                                </StackLayout>
                                            </local1:CustomViewCellFS>
                                        </DataTemplate>
                                    </local1:CustomListviewEventDetails.GroupHeaderTemplate>
                                    <local1:CustomListviewEventDetails.ItemTemplate>
                                        <DataTemplate>
                                            <ViewCell>
                                                <ViewCell.View>



                                                    <StackLayout Padding="0,0,0,0">
                                                        <Grid RowSpacing="0">
                                                            <Grid.RowDefinitions>
                                                                <RowDefinition Height="Auto"></RowDefinition>
                                                                <RowDefinition Height="Auto"></RowDefinition>
                                                                <RowDefinition Height="Auto"></RowDefinition>
                                                                <RowDefinition Height="5"></RowDefinition>
                                                            </Grid.RowDefinitions>
                                                            <Grid.ColumnDefinitions>
                                                                <ColumnDefinition Width="0.1*"></ColumnDefinition>
                                                                <ColumnDefinition Width="0.9*"></ColumnDefinition>
                                                            </Grid.ColumnDefinitions>
                                                            <!--Label Grid.Row="0" Grid.ColumnSpan="2" Text="{Binding SectionName}" FontSize="15" TextColor="Black" HorizontalOptions="C
                                                            enterAndExpand"></Label>-->
                                                            <!--<Label Grid.Row="1" Grid.ColumnSpan="2" Text="{Binding PackageName}" FontSize="15" TextColor="Black" HorizontalOptions="StartAndExpand"></Label>
                                                            -->
                                                            <Label Grid.Row="0" Grid.Column="0" Text="{Binding PackageQuantity}" FontSize="15"  TextColor="Black" HorizontalOptions="Start" IsVisible="{Binding PackageNameVisibility}" FontAttributes="Bold" >
                                                            </Label>
                                                            <Label Grid.Row="0" Grid.Column ="1" Text="{Binding PackageName}" FontSize="15"  TextColor="Black" HorizontalOptions="Start" HorizontalTextAlignment="Start" IsVisible="{Binding PackageNameVisibility}" FontAttributes="Bold" ></Label>
                                                            <Label Grid.Row="1" Grid.Column="0" Text="{Binding Quantity}" FontSize="15" TextColor="Black" HorizontalOptions="Start" HorizontalTextAlignment="Start"  >
                                                            </Label>
                                                            <Label  Grid.Row="1" Grid.Column="1" Text="{Binding Item}" FontSize="15"  TextColor="Black" HorizontalOptions="StartAndExpand" HorizontalTextAlignment="Start" Margin="{Binding MargProp}" >
                                                            </Label>
                                                            <!--<Label Grid.Row="2" Grid.Column="0" Text="{Binding SectionQuantity}" FontSize="15" TextColor="Black" HorizontalOptions="Start" HorizontalTextAlignment="Start" IsVisible="{Binding SectionItemVisibility}" >
                                                            </Label>
                                                            <Label  Grid.Row="2" Grid.Column="1" Text="{Binding SectionItem}" FontSize="15" TextColor="Black" HorizontalOptions="StartAndExpand" HorizontalTextAlignment="Start" IsVisible="{Binding SectionItemVisibility}" >
                                                            </Label>-->
                                                            <BoxView x:Name="txt" Grid.Row="3" Grid.ColumnSpan="2" HeightRequest="5" ></BoxView>

                                                        </Grid>
                                                    </StackLayout>
                                                </ViewCell.View>
                                            </ViewCell>
                                        </DataTemplate>
                                    </local1:CustomListviewEventDetails.ItemTemplate>
                                </local1:CustomListviewEventDetails>
                            </StackLayout>
                        </StackLayout>
                    </Grid>

                </StackLayout>

0

我将高度设置为3.0,但我希望高度是自动的。

我不太明白

  • 如果您将
    列表视图
    放在
    滚动视图
    中:坏主意<代码>列表视图本机支持滚动

  • 如果您想让
    滚动视图
    获得孩子的尺寸,比如使用
    android:layout\u height=“wrap\u content”
    线性布局
    :我认为只有像您这样通过编程才能实现


我已通过指定

 rdListViewHeight.Height = Content.Height;
而不是

 rdListViewHeight.Height = height * 3.0;
请提供更多代码(例如完整的xaml)