首页 > 解决方案 > 如果稍微倾斜 ios 设备,sfcombobox 会在 xamarin 表单中向上滚动

问题描述

如果稍微倾斜 ios 设备,sfcombobox 在 xamarin.forms 中向上滚动到顶部。这是在我们第一次进入页面时发生的。

<combobox:SfComboBox DataSource="{Binding SamplePickerList, Mode=TwoWay}" HorizontalOptions="FillAndExpand"  SelectedIndex="{Binding SamplePickerIndex, Mode=TwoWay}"
                  VerticalOptions="FillAndExpand" SelectedItem="{Binding SamplePicker,Mode=TwoWay}" />

标签: xamlxamarinxamarin.formsxamarin.iossyncfusion

解决方案


您可以通过使用 SfComboBox Renderer 并获取设备旋转和 UITableView 内容偏移位置来解决此问题。您可以通过查看其 ContentOffset 属性轻松获取 table view 的确切偏移量。

示例:https ://www.syncfusion.com/downloads/support/directtrac/general/ze/ComboBox_ScrollResetRotating-1020669557

代码片段:

public class CustomComboBoxRenderer : SfComboBoxRenderer
{
        /// <summary>
        /// Find device rotation.
       /// </summary>
       UIDeviceOrientation orientation;
      public CustomComboBoxRenderer()
      {
             Foundation.NSNotificationCenter.DefaultCenter.AddObserver(new NSString("UIDeviceOrientationDidChangeNotification"), this.DeviceRotated);
      }
      private async void DeviceRotated(NSNotification obj)
      {
            if (this.Control.Text == string.Empty && this.Control.IsSuggestionOpen)
            {
                //// Get the table view position.
                var offset = this.Control.AutoCompleteTableView.TableView.ContentOffset;
                await System.Threading.Tasks.Task.Delay(1);
                //// Set the table view position while rotating.
                this.Control.AutoCompleteTableView.TableView.ContentOffset = offset;
            }
     }
} 

在此示例中,我们使用 UIDeviceOrientation 保存旋转前的滚动位置,然后在设备倾斜后滚动到该行。


推荐阅读