首页 > 解决方案 > 是否可以使用 xamarin 表单向下拖动面板滑块?

问题描述

我是 xamamrin 表单的新手,任何人都可以帮助我拖动到下面板滑块。我想使用图像中提到的xamarin 表单在 android 中向下拖动滑块

任何有想法的请分享野兔。

标签: androidiosxamarinpanelslide

解决方案


有很多方法可以实现滑动面板,我提供了其中一种使用PanGestureRecognizer

在xml中

<?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>

在后面的代码中


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;
            }
        }
    }
}

在此处输入图像描述


推荐阅读