首页 > 解决方案 > 应用程序加载时如何首先显示第一个网格行

问题描述

看来我的网格布局设置为第二行首先显示。

当应用程序加载 Grid.Row="2" 时,页面的中间部分首先显示。我必须向下滚动才能看到轮播女巫 Grid.Row="1" 我试图找出顶部的轮播然后是 Change 程序的视频。

这是我的 xaml 代码。

 <ScrollView HorizontalOptions="Center">
 <Grid x:Name="page" Padding="0,0"> 
 <Grid.RowDefinitions>
 <RowDefinition Height="600" /> 
 <RowDefinition Height="Auto" /> 
 <RowDefinition Height="Auto" />
 </Grid.RowDefinitions>
<StackLayout Grid.Row="0"> 
 <CarouselView x:Name="carousel" Scrolled="Handle_Scrolled" ItemsSource="{Binding Items}" 
  VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"> 
<CarouselView.Behaviors> 
<behaviors:FrontBannerViewParallaxBehavior ParallaxOffset="100"/> 
</CarouselView.Behaviors>
<CarouselView.ItemTemplate> <DataTemplate> <Grid Padding="18,24,18,64"> <custom:PancakeView 
CornerRadius="24" BackgroundColor="#FFFFFF"> <StackLayout VerticalOptions="Fill"     
HorizontalOptions="Fill"> <custom:PancakeView VerticalOptions="FillAndExpand" HeightRequest="200" 
CornerRadius="10" BackgroundGradientStartColor="{Binding StartColor}"       
BackgroundGradientEndColor="{Binding EndColor}"> <Grid> <StackLayout> <StackLayout 
Orientation="Horizontal"> <Label Text="{Binding Title}" FontFamily="{StaticResource 
font_extrabold}" HorizontalOptions="Center" TextColor="#60229a" VerticalOptions="End" 
 Margin="14,0,0,0" FontSize="36" FontAttributes="Bold" LineBreakMode="NoWrap" /> </StackLayout> 
<Label FontFamily="{StaticResource font_regular}" Text="{Binding Details}" TextColor="#60229a" 
Margin="14,8,24,24" FontSize="22" LineBreakMode="WordWrap" /> </StackLayout> </Grid> 
 </custom:PancakeView> </StackLayout> </custom:PancakeView> <Image Source="{Binding ImageSrc}" 
  WidthRequest="{Binding Width}" VerticalOptions="EndAndExpand" HorizontalOptions="EndAndExpand" 
  Margin="0,0,15,0" /> </Grid> </DataTemplate> </CarouselView.ItemTemplate> 
 </CarouselView> 
 </StackLayout>
 <StackLayout Grid.Row="1">`
       `<StackLayout Orientation="Horizontal">
            <Label Text="Listen to Apostle Johnson's" FontFamily="{StaticResource 
font_extrabold}" HorizontalOptions="Center" TextColor="#FFFFFF" VerticalOptions="End" Margin="14,0,0,0" FontSize="36" FontAttributes="Bold" LineBreakMode="NoWrap" />
        </StackLayout>
        <Label FontFamily="{StaticResource font_regular}"  Text="Detox Transformation." TextColor="#FFFFFF" Margin="14,8,24,24" FontSize="22" LineBreakMode="WordWrap" />
    <StackLayout Orientation="Horizontal">
            <Label FontFamily="{StaticResource font_regular}" Text="Apostle Johnson gives a 
riveting short bio of his testimony of traveling a road of living a life of descriptive chaos.  Hey just click on the video and hear it from him." TextColor="#FFFFFF" Margin="14,8,24,24" FontSize="22" LineBreakMode="WordWrap" />
        </StackLayout>
        <StackLayout Orientation="Horizontal">
            <ovideo:VideoPlayer x:Name="vmo" Source="http://iowegodministry.org/wp-content/uploads/2020/06/y2mate.com-FROM-GANGSTA-TO-GOD_JeR29HVa7HE_360p.mp4" WidthRequest="250" />
        </StackLayout>
    </StackLayout> 
    <StackLayout Grid.Row="2">
        <StackLayout Orientation="Horizontal">
            <Label Text="Chance Program" FontFamily="{StaticResource font_extrabold}" HorizontalOptions="Center" TextColor="#FFFFFF" VerticalOptions="End" Margin="14,0,0,0" FontSize="36" FontAttributes="Bold" LineBreakMode="NoWrap" />
        </StackLayout>
        <Label FontFamily="{StaticResource font_regular}"  Text="The mission of C.H.A.N.C.E. is to help people succeed in the workforce by providing training and mentorship.  One of the goals is to help those who've been in the prison system become successful members of society after they get out.  The program will provide clas`enter code here`ses on a variety of spiritual-based educational curriculum, teach job interview skills, and help with strengthening family relationships and bonds." TextColor="#FFFFFF" Margin="14,8,24,24" FontSize="22" LineBreakMode="WordWrap" />
        <Button BackgroundColor="#FFFFFF" 
                FontFamily="{StaticResource font_bold}" 
                Padding="20,0" CornerRadius="20" 
                TextColor="{Binding BackgroundColor}" 
                FontSize="12" BorderColor="{Binding BackgroundColor}" 
                BorderWidth="2" HeightRequest="40" 
                Text="Read More About the Program" HorizontalOptions="Center" Clicked="OnButtonClicked"
                />
    </StackLayout>
</Grid>
  

   
 </ScrollView>

标签: c#xamarin.forms

解决方案


来自共享Xaml代码,用作ScrollViewRootLayout。ScrollView 有一个ScrollToAsync方法可以滚动到 View 的一个位置。您可以尝试在应用程序加载时滚动到您想要的位置。

可以使用接受 double和argumentsScrollView的方法滚动到a 中的位置。ScrollToAsyncxy

ScrollView给定一个名为scrollView的垂直对象,以下示例显示如何150从顶部滚动到与设备无关的单元ScrollView

await scrollView.ScrollToAsync(0, 150, true);

推荐阅读