首页 > 解决方案 > 在iOS中的Xamarin.Forms中,长按进入键盘后面?

问题描述

我有聊天页面。Page 使用 StackLayout 作为 ListView、Entry 和 Button 的包装器。

聊天页面.xaml

<ContentPage.Resources>
<ResourceDictionary>
  <local:MessageTemplateSelector x:Key="MessageTemplateSelector" />
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<StackLayout>
  <ListView x:Name="MessagesListView"
            ItemTemplate="{StaticResource MessageTemplateSelector}"
            ItemsSource="{Binding Messages}"
            HasUnevenRows="True"
            IsPullToRefreshEnabled="true"
            IsRefreshing="{Binding IsRefreshing}"
            RefreshCommand="{Binding RefreshCommand}"
            SeparatorVisibility="None"
            RelativeLayout.HeightConstraint="{ConstraintExpression Type=RelativeToParent,Property=Height,Factor=1,Constant=0}"
    />

  <Grid x:Name="MessageControls" RowSpacing="1" ColumnSpacing="2" Padding="5"
        BackgroundColor="#EFEFF4"
        VerticalOptions="FillAndExpand"
        HorizontalOptions="FillAndExpand">
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*" />
      <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

    <Entry x:Name="txtMessage" Grid.Column="0" HeightRequest="40" Placeholder="Message" Text="{Binding OutGoingText}" TextChanged="EnableSend"/>

    <Button x:Name="sendButton" Grid.Column="1" Text="Send" Command="{Binding SendCommand}"/>
  </Grid>
</StackLayout>
</ContentPage.Content>

问题:当我单击条目时,键盘覆盖条目。所以,我已经处理了键盘出现/消失事件来管理条目可见性,如下面的代码。除了这种情况,它工作正常。每当用户长按条目(最有可能复制/粘贴)时,条目就会向下/在键盘后面。

有人可以建议我吗?

[assembly: ExportRenderer (typeof (ChatPage), typeof (KeyboardAdaptedPageRenderer))]
namespace Project.iOS
{
    public class KeyboardAdaptedPageRenderer : ContentPageWithCustomBackButtonRenderer
    {
        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);

            Keyboard.WillShow += OnKeyboardWillShow;
            Keyboard.WillHide += OnKeyboardWillHide;
        }

        public override void ViewDidDisappear(bool animated)
        {
            base.ViewDidDisappear(animated);

            Keyboard.WillShow -= OnKeyboardWillShow;
            Keyboard.WillHide -= OnKeyboardWillHide;

            OnKeyboardWillHide(new KeyboardInfo()
            {
                AnimationDuration = 0,
                AnimatonOptions = UIViewAnimationOptions.TransitionNone
            });
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            Keyboard.WillShow -= OnKeyboardWillShow;
            Keyboard.WillHide -= OnKeyboardWillHide;
        }

        private void OnKeyboardWillShow(KeyboardInfo info)
        {
            if (info.SoftwareKeyboardIsVisible)
            {
                UIView.Animate(info.AnimationDuration, 0, info.AnimatonOptions, () =>
                {
                    var bounds = View.Bounds;
                    bounds.Y = info.BeginRect.Top - info.EndRect.Top; // iphone 4 and others
                    View.Bounds = bounds;
                }, null);
            }
        }

        private void OnKeyboardWillHide(KeyboardInfo info)
        {
            UIView.Animate(info.AnimationDuration, 0, info.AnimatonOptions, () =>
            {
                var bounds = View.Bounds;
                bounds.Y = 0;
                View.Bounds = bounds;
            }, null);
        }
    }
}

标签: xamarinxamarin.formsxamarin.ioschat

解决方案


推荐阅读