【问题标题】:How to collapse (hide or swipe up) Navigation Bar (Title Bar) while scrolling in Xamarin Forms?如何在 Xamarin Forms 中滚动时折叠(隐藏或向上滑动)导航栏(标题栏)?
【发布时间】:2021-09-09 19:02:59
【问题描述】:

我有一个ContentPage,上面有一个包含页面标题的导航栏(标题栏)。我希望导航栏在内容(可能是ListView)向上滚动时折叠,并且导航栏必须在内容向下滚动时再次出现(向下滑动)。

请注意,我使用的是FlyoutPage,以前称为MasterDetailPage。

这是我的 XAML 代码

<ContentPage ...
             NavigationPage.HasNavigationBar="True"
             Title="Home">
    <ContentPage.Content>

        <ListView VerticalOptions="FillAndExpand" HasUnevenRows="True" CachingStrategy="RecycleElement"
                        ItemsSource="{Binding ListItems}" SeparatorVisibility="None">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout...>
                            <Label... />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

    </ContentPage.Content>
</ContentPage>

与此类似:

如何在 Xamarin.Forms 中实现这一点?

【问题讨论】:

    标签: c# xaml listview xamarin xamarin.forms


    【解决方案1】:

    步骤:

    1. 伪造标题栏并设置参考名称。
    2. 附加滚动事件。
    3. 添加动画。

    查看示例代码或sample project here。

    XAML 页面

    <ContentPage NavigationPage.HasNavigationBar="False">
        <ContentPage.Content>
            <StackLayout Spacing="0">
                <StackLayout
                    x:Name="TitleLayout"
                    BackgroundColor="#2296f3"
                    HeightRequest="58"
                    Orientation="Horizontal">
                    <Label
                        Margin="40,0,0,0"
                        FontSize="20"
                        Text="Title"
                        TextColor="White"
                        VerticalOptions="CenterAndExpand" />
                </StackLayout>
                <ListView ...
                          Scrolled="ScrollView_Scrolled" >
                </ListView>
            </StackLayout>
        </ContentPage.Content>
    </ContentPage>
    

    XAML.cs 类

            private double previousOffset;
    
            private async void ScrollView_Scrolled(object sender, ScrolledEventArgs e)
            {
                double translation;
                bool visibility;
    
                if (previousOffset < e.ScrollY - 45) //scroll sensitivity
                {
                    translation = -58; //same Title bar height
                    visibility = false;
                }
                else if (previousOffset > e.ScrollY + 45)
                {
                    translation = 0;
                    visibility = true;
                }
                else
                {
                    return;
                }
    
                await TitleLayout.TranslateTo(TitleLayout.TranslationX, translation, 300);
                await Task.Delay(100);
                TitleLayout.IsVisible = visibility;
                previousOffset = e.ScrollY;
            }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-30
      • 2018-12-10
      • 1970-01-01
      • 2017-03-03
      • 2023-04-11
      • 1970-01-01
      • 2016-12-27
      相关资源
      最近更新 更多