You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin SwipeView多次滑动后关闭返回最后一次滑动起始位置异常

Xamarin SwipeView 滑动复位异常解决方案

问题表现

在已经处于展开状态的SwipeView元素上执行多次滑动操作后,关闭该元素时,它会自动返回到最后一次滑动的起始位置,而非默认的初始位置。
问题效果演示:
问题效果演示动图

解决方案

这是Xamarin.Forms SwipeView的已知控件逻辑缺陷,可通过以下任意一种方式修复:

  • 方案1:手动监听SwipeView关闭事件强制复位
    给SwipeView绑定CloseRequested事件,事件处理逻辑如下:
    private void OnSwipeViewCloseRequested(object sender, EventArgs e)
    {
        var swipeView = sender as SwipeView;
        if (swipeView != null)
        {
            swipeView.OpenSwipeItem = OpenSwipeItem.None;
        }
    }
    
    XAML中对应修改SwipeView声明:
    <SwipeView CloseRequested="OnSwipeViewCloseRequested">
    
  • 方案2:升级Xamarin.Forms版本
    该问题已经在Xamarin.Forms 5.0 Service Release 10及后续版本中修复,直接升级对应Nuget包即可解决。
  • 方案3:临时规避方案
    如果无法升级框架版本,也可以在SwipeView的SwipeEnded事件中判断滑动状态,当检测到滑动结束且无选中菜单项时,手动将SwipeView的偏移重置为0。

问题复现代码参考

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"
             x:Class="TestSwipeView.MainPage">
<CollectionView x:Name="collectionView"
                ItemsSource="{Binding TestSequence}">
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <SwipeView>
                <SwipeView.LeftItems>
                    <SwipeItems>
                        <SwipeItem Text="F"
                                   BackgroundColor="LightGreen"/>
                        <SwipeItem Text="D" 
 BackgroundColor="LightPink"/>
                    </SwipeItems>
                </SwipeView.LeftItems>
                <Grid BackgroundColor="White"
                      Padding="10">
                    <Label Text="{Binding .}"></Label>
                </Grid>
            </SwipeView>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>
</ContentPage>

后台C#代码

namespace TestSwipeView
{
    public partial class MainPage : ContentPage
    {
        public List<String> TestSequence { get; set; }
        public MainPage()
        {
            TestSequence = new List<string>();
            for (int i=0; i<10; i++)
            {
                TestSequence.Add(String.Format("Test {0}",i));
            }
            InitializeComponent();
            this.BindingContext = this;
        }
    }
}

内容的提问来源于stack exchange,提问作者Adventure Mania

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.09 05:15:00