是否可以将 SwipeView 添加到网格行是 Xamarin Forms

Is it possible to add a SwipeView to a grid row is Xamarin Forms

我正在我的 Xamarin Forms 应用程序的 C# 代码中构建一个网格。我想为用户提供在一行上滑动并显示 swipeview 托盘的能力。我已尝试按照此处的文档 - https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/swipeview.

下面是我的代码:

   private void AddSwipeControl(int pi_RowNumber)
    {
        try
        {
            //Add the swipeview
            //Add a swip control to the and have it go all the way across.  See if this works
            // SwipeItems
            SwipeItem tobj_DeleteSwipeItem = new SwipeItem
            {
                Text = "Delete",
                BackgroundColor = Color.LightGreen
            };

            tobj_DeleteSwipeItem.Invoked += tobj_DeleteSwipeItem_Invoked; ;
            List<SwipeItem> tobj_swipeItems = new List<SwipeItem>() { tobj_DeleteSwipeItem };

            // SwipeView content
            Label swipeContent = new Label()
            {
                BackgroundColor = Color.Pink,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand
            };

            var tobj_SwipeGestureRecognizer = new SwipeGestureRecognizer() { Direction = SwipeDirection.Right };
            tobj_SwipeGestureRecognizer.Swiped += tobj_SwipeGestureRecognizer_Swiped;
            swipeContent.GestureRecognizers.Add(tobj_SwipeGestureRecognizer);

            SwipeView swipeView = new SwipeView
            {
                LeftItems = new SwipeItems(tobj_swipeItems) { Mode = SwipeMode.Reveal },
                Content = swipeContent
            };

            //Add the swipeview to the grid
            grdDataGrid.Children.Add(swipeView, 0, pi_RowNumber);
            Grid.SetColumnSpan(swipeView, grdDataGrid.ColumnDefinitions.Count);
        }
        catch (Exception ex)
        {
            SharedErrorHandler.ProcessException(ex);
        }
    }

    private void tobj_SwipeGestureRecognizer_Swiped(object sender, SwipedEventArgs e)
    {
        try
        {
            if (e.Direction == SwipeDirection.Right)
            {
                var tobj_SwipeView = ((Label)sender).Parent as SwipeView;
                tobj_SwipeView.Open(OpenSwipeItem.LeftItems);
            }
        }
        catch (Exception ex)
        {
            SharedErrorHandler.ProcessException(ex);
        }
    }

我可以在我的网格行中准确地看到我想要的内容,并且标签控件的滑动事件按预期触发。但是,即使我尝试使用 tobj_SwipeView.Open(OpenSwipeItem.LeftItems) 以编程方式打开它,滑动托盘也永远不会打开;这让我觉得我在构建滑动视图时做错了什么。我已经在 Android 和 UWP 上进行了测试。知道我做错了什么吗?

更新:所以这似乎只是对 UWP 的挑战。下面是我更新的代码。我在屏幕上有一个按钮,我按下它可以 运行 Button_Clicked 事件。

    private void tobj_SwipeGestureRecognizer_Swiped(object sender, SwipedEventArgs e)
    {
        try
        {
            if (e.Direction == SwipeDirection.Right)
            {
                var tobj_SwipeView = ((Label)sender).Parent as SwipeView;
                tobj_SwipeView.Open(OpenSwipeItem.LeftItems);
            }
        }
        catch (Exception ex)
        {
            //SharedErrorHandler.ProcessException(ex);
            System.Diagnostics.Debug.WriteLine(ex.ToString());
        }
    }

    private void Button_Clicked(object sender, EventArgs e)
    {
        try
        {
            //First add two rows
            grdDataGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
            grdDataGrid.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });

            //First add some data columns to the grid
            grdDataGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto});
            grdDataGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
            grdDataGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });


            //Add data to each row
            grdDataGrid.Children.Add(new Label() { Text = "Row 0 Col 0" }, 0 , 0);
            grdDataGrid.Children.Add(new Label() { Text = "Row 0 Col 1" }, 1, 0);
            grdDataGrid.Children.Add(new Label() { Text = "Row 0 Col 2" }, 2, 0);

            grdDataGrid.Children.Add(new Label() { Text = "Row 1 Col 0" }, 0, 1);
            grdDataGrid.Children.Add(new Label() { Text = "Row 1 Col 1" }, 1, 1);
            grdDataGrid.Children.Add(new Label() { Text = "Row 1 Col 2" }, 2, 1);

            var ti_RowNumber = -1;
            foreach (RowDefinition tobj_Row in grdDataGrid.RowDefinitions)
            {
                ti_RowNumber += 1;

                //Add the swipeview
                //Add a swip control to the and have it go all the way across.  See if this works
                // SwipeItems
                SwipeItem tobj_DeleteSwipeItem = new SwipeItem
                {
                    Text = "Delete",
                    BackgroundColor = Color.LightGreen
                };

                tobj_DeleteSwipeItem.Invoked += OnDeleteSwipeItemInvoked; ;
                List<SwipeItem> tobj_swipeItems = new List<SwipeItem>() { tobj_DeleteSwipeItem };   //comment out

                // SwipeView content
                Label swipeContent = new Label()
                {
                    BackgroundColor = Color.Pink,
                    Text = "test",
                    HorizontalOptions = LayoutOptions.FillAndExpand,
                    VerticalOptions = LayoutOptions.FillAndExpand
                };

                var tobj_SwipeGestureRecognizer = new SwipeGestureRecognizer() { Direction = SwipeDirection.Right };
                tobj_SwipeGestureRecognizer.Swiped += tobj_SwipeGestureRecognizer_Swiped;
                swipeContent.GestureRecognizers.Add(tobj_SwipeGestureRecognizer);

                SwipeView swipeView = new SwipeView
                {
                    LeftItems = new SwipeItems(tobj_swipeItems) { Mode = SwipeMode.Reveal },
                    Content = swipeContent
                };

                //Add the swipeview to the grid
                grdDataGrid.Children.Add(swipeView, 0, ti_RowNumber);
                Grid.SetColumnSpan(swipeView, grdDataGrid.ColumnDefinitions.Count);
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.ToString());
        }
    }

这似乎在 iOS 和 Android 上运行良好,但在 UWP 上,标签的滑动事件会触发,但滑动托盘永远不会打开。这是一个已知的 UWP 挑战吗?

我根据您的代码创建了一个简单的演示,并且在我这边运行正常。

您可以参考以下代码:

   <StackLayout Margin="20">
        <Grid    x:Name="grdDataGrid"   VerticalOptions="FillAndExpand">
            <Grid.RowDefinitions  >
                <RowDefinition Height="60"  />
                <RowDefinition Height="60" />
            </Grid.RowDefinitions>


        <Button  Text="add" Clicked="Button_Clicked" />
    </StackLayout>

C#代码是:

    private void AddSwipeControl()
    {
        try
        {
            //Add the swipeview
            //Add a swip control to the and have it go all the way across.  See if this works
            // SwipeItems
            SwipeItem tobj_DeleteSwipeItem = new SwipeItem
            {
                Text = "Delete",
                BackgroundColor = Color.LightGreen
            };

            tobj_DeleteSwipeItem.Invoked += OnDeleteSwipeItemInvoked; ;
            List<SwipeItem> tobj_swipeItems = new List<SwipeItem>() { tobj_DeleteSwipeItem };   //comment out

            //SwipeItems tobj_swipeItems = new SwipeItems();
            //tobj_swipeItems.Add(tobj_DeleteSwipeItem);

            // SwipeView content
            Label swipeContent = new Label()
            {
                BackgroundColor = Color.Pink,
                Text = "test",
                HorizontalOptions = LayoutOptions.FillAndExpand,
                VerticalOptions = LayoutOptions.FillAndExpand
            };

            var tobj_SwipeGestureRecognizer = new SwipeGestureRecognizer() { Direction = SwipeDirection.Right };
            tobj_SwipeGestureRecognizer.Swiped += tobj_SwipeGestureRecognizer_Swiped;
            swipeContent.GestureRecognizers.Add(tobj_SwipeGestureRecognizer);

            SwipeView swipeView = new SwipeView
            {
                LeftItems = new SwipeItems(tobj_swipeItems) { Mode = SwipeMode.Reveal },
                Content = swipeContent
            };

            //Add the swipeview to the grid
            grdDataGrid.Children.AddVertical(swipeView);

           // grdDataGrid.Children.Add(swipeView, 0, pi_RowNumber);
           // Grid.SetColumnSpan(swipeView, grdDataGrid.ColumnDefinitions.Count);
        }
        catch (Exception ex)
        {
           // SharedErrorHandler.ProcessException(ex);

            System.Diagnostics.Debug.WriteLine(ex.ToString());
        }
    }

    private void tobj_SwipeGestureRecognizer_Swiped(object sender, SwipedEventArgs e)
    {
        try
        {
            if (e.Direction == SwipeDirection.Right)
            {
                var tobj_SwipeView = ((Label)sender).Parent as SwipeView;
                tobj_SwipeView.Open(OpenSwipeItem.LeftItems);
            }
        }
        catch (Exception ex)
        {
            //SharedErrorHandler.ProcessException(ex);
            System.Diagnostics.Debug.WriteLine(ex.ToString());
        }
    }

注:

SwipeControl添加到Grid时,我使用了代码:

grdDataGrid.Children.AddVertical(swipeView);

为了简化代码,我删除了函数AddSwipeControl()的参数。