如何自动调整ScrollView的大小以适合xamarin Forms中的内容

时间:2018-05-05 09:11:34

标签: listview xamarin.forms scrollview

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

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>

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

2 个答案:

答案 0 :(得分:1)

我不太了解。

  • 如果你在ListView中加ScrollView:不好主意。 ListView原生支持滚动。

  • 如果您希望ScrollView获取孩子的维度,例如带有LinearLayout的{​​{1}}:我认为它只能像您一样以编程方式实现。< / p>

答案 1 :(得分:1)

我已通过指定

修复了此问题
 rdListViewHeight.Height = Content.Height;

而不是

 rdListViewHeight.Height = height * 3.0;