【问题标题】:Is it possible to drag to down panel slider as real android system slider using xamarin forms?是否可以使用 xamarin 表单将面板滑块作为真正的 android 系统滑块拖动到下面板?
【发布时间】:2019-12-23 05:28:49
【问题描述】:

我已经关注了here 中的滑块,但我想实现真正的 android 滑块,如下面的图像拖动到下所述。

同(向下滑动):

same as(drag to down slide)

任何人有想法请在这里分享。我尝试了很多,但都没有成功。

@Lucas Zhang,有你的想法吗?

【问题讨论】:

    标签: android ios xamarin slider


    【解决方案1】:

    在xml中

    <AbsoluteLayout BackgroundColor="White" AbsoluteLayout.LayoutBounds="0,1,1,1">
       <!--  -->
       <StackLayout x:Name="bottomDrawer" BackgroundColor="Olive" AbsoluteLayout.LayoutBounds="0.5,1.00,0.9,0.04" AbsoluteLayout.LayoutFlags="All">
       <StackLayout.GestureRecognizers>
            <PanGestureRecognizer PanUpdated="PanGestureHandler" />
       </StackLayout.GestureRecognizers>
    
       <!-- put the content of panel slider here -->
    
       </StackLayout>
    </AbsoluteLayout>
    

    在后面的代码中

        double? layoutHeight;
        double layoutBoundsHeight;
        int direction;
        const double layoutPropHeightMax = 0.75;
        const double layoutPropHeightMin = 0.04;
        void PanGestureHandler(object sender, PanUpdatedEventArgs e)
        {
            layoutHeight = layoutHeight ?? ((sender as StackLayout).Parent as AbsoluteLayout).Height;
            switch (e.StatusType)
            {
                case GestureStatus.Started:
                    layoutBoundsHeight = AbsoluteLayout.GetLayoutBounds(sender as StackLayout).Height;
                    break;
                case GestureStatus.Running:
                    direction = e.TotalY < 0 ? 1 : -1;
                    break;
                case GestureStatus.Completed:
                    if (direction > 0)
                    {
                        Device.BeginInvokeOnMainThread(async () =>
                        {
    
                            var height = layoutPropHeightMin;
    
                            while (height < layoutPropHeightMax)
                            {
                                await Task.Delay(2);
                                height += 0.04;
    
                                AbsoluteLayout.SetLayoutBounds(bottomDrawer, new Rectangle(0.5, 1.00, 0.9, height));
                            }
    
                        });
    
                    }
                    else
                    {
                        Device.BeginInvokeOnMainThread(async () =>
                        {
    
                            var height = layoutPropHeightMax;
    
                            while (height > layoutPropHeightMin)
                            {
                                await Task.Delay(2);
                                height -= 0.04;
    
                                AbsoluteLayout.SetLayoutBounds(bottomDrawer, new Rectangle(0.5, 1.00, 0.9, height));
                            }
    
                        });
                    }
                    break;
            }
        }
    

    注意:

    因为我们使用了 AbsoluteLayout 。当您想在面板中添加子控件时,最好设置AbsoluteLayout.LayoutBounds 而不是HeightRequestWidthRequest

    【讨论】:

    • 每当我们向下拖动时,我们可以拖动“AbsoluteLayout”部分吗?现在我只能滑动我的内容
    • 你为什么要这样做?
    • 当然你可以用AbsoluteLayout代替StackLayout
    • 感谢您的回答,如果可能的话,请提供少量代码非常好
    • 好的,但是一旦它补充了滑块,我想隐藏滑块部分现在像i.stack.imgur.com/CeT0u.png一样为我工作,如果可能的话,你能帮我写代码吗
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    • 2022-12-11
    • 2020-05-15
    • 1970-01-01
    • 2014-08-20
    • 1970-01-01
    相关资源
    最近更新 更多