You need to enable JavaScript to run this app. Xamarin.Forms中CarouselView内Entry输入时自动滚回首项如何解决
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin.Forms中CarouselView内Entry输入时自动滚回首项如何解决

问题原因与解决步骤
  • 触发滚动重置的核心原因是:Android平台上Entry获取焦点弹出软键盘时,会触发CarouselView的布局重计算,而当前实现未将CarouselView的CurrentItem、Position属性与ViewModel双向绑定,导致布局重算后滚动位置被默认重置到索引0。
  • 配套的次要问题包括:Entry未绑定数据源属性,输入内容无法持久化;ItemTemplate使用DynamicResource可能触发模板重加载;Model未实现属性变更通知,绑定逻辑无法正常工作。

解决步骤

1. 扩展Model,支持输入值存储与属性通知

给Tour_Tag新增用户输入的属性,实现INotifyPropertyChanged接口:

public class Tour_Tag : INotifyPropertyChanged
{
    private string _userInputValue;
    /// <summary>用户录入的设备数值</summary>
    public string UserInputValue
    {
        get => _userInputValue;
        set
        {
            _userInputValue = value;
            OnPropertyChanged(nameof(UserInputValue));
        }
    }

    // 原有属性保留
    public Guid Tour_Id { get; set; }
    public string Limit_Type { get; set; }
    public string Parameter { get; set; }
    public string Tag_Name_Minimum_Expected { get; set; }
    public string Tag_Name_Maximum_Expected { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

2. 调整ViewModel的属性绑定逻辑

确保CurrentItem、Position属性支持变更通知,用于CarouselView的双向绑定:

public class Tour_TagViewModel : INotifyPropertyChanged
{
    private Tour_Tag _currentItem;
    /// <summary>CarouselView当前选中项</summary>
    public Tour_Tag CurrentItem
    {
        get => _currentItem;
        set
        {
            _currentItem = value;
            OnPropertyChanged(nameof(CurrentItem));
        }
    }

    private int _position;
    /// <summary>CarouselView当前滚动位置</summary>
    public int Position
    {
        get => _position;
        set
        {
            _position = value;
            OnPropertyChanged(nameof(Position));
        }
    }

    // 原有其他属性与逻辑保留
    readonly IList<Tour_Tag> source;
    public ObservableCollection<Tour_Tag> Get_Tour_Run_Tags { get; private set; }

    public Tour_TagViewModel()
    {
        source = new List<Tour_Tag>();
        CreateTourTagsCollection();
        // 初始滚动位置设置
        CurrentItem = Get_Tour_Run_Tags.Skip(3).FirstOrDefault();
        Position = 3;
    }

    // 原有OnPropertyChanged、CreateTourTagsCollection、事件定义保留
    private void OnPropertyChanged(string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    void CreateTourTagsCollection()
    {
        // 原有初始化逻辑保留
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

3. 修改XAML配置,固定滚动位置并绑定输入

  • 给CarouselView添加CurrentItem、Position的双向绑定,添加ItemsUpdatingScrollMode="KeepCurrentItemInView"属性,避免布局变化时滚动位置重置
  • 将DynamicResource改为StaticResource,避免资源重解析触发模板重载
  • 给两个DataTemplate内的Entry添加Text双向绑定,存储用户输入的内容

修改后的CarouselView代码:

<CarouselView 
    IndicatorView="indicatorView" 
    ItemsSource="{Binding Get_Tour_Run_Tags}"  
    ItemTemplate="{StaticResource mainTemplate}"
    CurrentItem="{Binding CurrentItem, Mode=TwoWay}"
    Position="{Binding Position, Mode=TwoWay}"
    ItemsUpdatingScrollMode="KeepCurrentItemInView"
    x:Name="lstData">
</CarouselView>

参数模板内的Entry修改:

<Entry   
    FontSize="Medium" 
    WidthRequest="250" 
    HorizontalTextAlignment="Center" 
    HorizontalOptions="Center" 
    VerticalOptions="Center"
    Text="{Binding UserInputValue, Mode=TwoWay}"
    x:Name="entryPosicion"/>

范围模板内的Entry修改:

<Entry   
    FontSize="Medium" 
    WidthRequest="250" 
    HorizontalTextAlignment="Center" 
    HorizontalOptions="Center" 
    VerticalOptions="Center"
    Text="{Binding UserInputValue, Mode=TwoWay}"
    x:Name="entryPosicion"/>

可选优化:适配Android软键盘模式

如果修改后仍出现滚动重置,可在Android平台的MainActivity.cs中调整软键盘弹出模式,避免布局压缩触发重计算:
在OnCreate方法的InitializeComponent()调用前添加:

Window.SetSoftInputMode(Android.Views.SoftInput.AdjustPan);

该设置会让软键盘弹出时直接平移页面,不压缩原有布局尺寸。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.22 08:15:08