【问题标题】:Is there a way to prevent a Xamarin Forms scrollview from automatically scrolling to the last edited Entry when a picker control is opened?有没有办法防止 Xamarin Forms 滚动视图在打开选择器控件时自动滚动到最后编辑的条目?
【发布时间】:2020-11-12 07:35:41
【问题描述】:

我有一个类似这样的设置:

  <ScrollView>
    <StackLayout>    
      <Entry/>
      <Picker/>
      <Entry/>
      <Entry/>
      <Entry/>
      <Picker/>
      <Picker/>
      <Entry/>
      <Entry/>
      <Entry/>
    </StackLayout>

  </ScrollView>

Entry 和 Picker 控件是动态创建和修改的。问题是,如果我输入一个条目,然后打开其中一个 Picker 控件,则 ScrollView 会滚动到先前编辑的条目所在的位置。它正在获得焦点,但我阻止了焦点转移,但 ScrollView 滚动,我似乎无法阻止它。

我做了类似于Is it possible to stop the first Entry getting Focus in a ScrollView in Xamarin Forms 的操作来修复UWP 中的相关问题,但该解决方案不适用于iOS 和Android。

【问题讨论】:

  • 我用 xaml 创建了这个项目。我输入了一个条目,然后打开了一个 Picker 控件,ScrollView 没有滚动到先前编辑的条目所在的位置。你可以看看截图。 imgur.com/o3sS039你能提供更多细节吗?
  • 您可以尝试回复中的代码来重置焦点。您能否提供您的设备版本和 xamarin.form 版本?

标签: c# android ios xamarin xamarin.forms


【解决方案1】:

设置StackLayout的名称。 Foreach 'StackLayout' 中的元素并重置焦点。

 private void Picker_Focused(object sender, FocusEventArgs e)
    {
        foreach (var item in stackLayout.Children)
        {
            if (sender!=item)
            {
                item.Unfocus();                   
            }              
        }
    }

【讨论】:

  • 仍然滚动到控件,即使它没有获得焦点。我以前试过这个,但为了完整起见我又试了一次。对我不起作用。
  • 滚动发生在 选择器的 Focus 事件触发并完成之后。
猜你喜欢
  • 1970-01-01
  • 2014-08-12
  • 2015-11-01
  • 2021-09-08
  • 2016-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-24
相关资源
最近更新 更多