Is it possible to drag to down panel slider using xamarin forms?

时间秒杀一切 提交于 2020-01-11 07:58:06

问题


I'm new in xamamrin forms anyone helps me about drag to down panel slider. I want to drag to down slider in android using xamarin forms mentioned in image

Any have idea about please share hare.


回答1:


There are many way to implement a sliding panel, I provide one of them which using a PanGestureRecognizer :

in xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"         
             mc:Ignorable="d"
             x:Class="App10.MainPage">

    <AbsoluteLayout BackgroundColor="White" AbsoluteLayout.LayoutBounds="0,1,1,1">
        <!--  -->
        <StackLayout x:Name="bottomDrawer" BackgroundColor="Olive" AbsoluteLayout.LayoutBounds="0.5,0.00,0.9,0.04" AbsoluteLayout.LayoutFlags="All">
            <StackLayout.GestureRecognizers>
                <PanGestureRecognizer PanUpdated="PanGestureHandler" />
            </StackLayout.GestureRecognizers>

            <!-- put the content of panel slider here -->

        </StackLayout>
    </AbsoluteLayout>

</ContentPage>

in code behind


using System.ComponentModel;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace App10
{
    // Learn more about making custom code visible in the Xamarin.Forms previewer
    // by visiting https://aka.ms/xamarinforms-previewer
    [DesignTimeVisible(false)]
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();        
        }

        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, 0.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, 0.00, 0.9, height));
                            }

                        });
                    }
                    break;
            }
        }
    }
}



来源:https://stackoverflow.com/questions/59387524/is-it-possible-to-drag-to-down-panel-slider-using-xamarin-forms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!