当用户单击Xamarin.Form中的Entry时,ListView保留空间(在iOS中检查)

时间:2018-06-19 13:37:48

标签: listview xamarin xamarin.forms xamarin.ios

我有在聊天页面存在的应用程序。在页面上,我将redFill = PatternFill(start_color='EE1111',end_color='EE1111', fill_type='solid') ws1.conditional_formatting.add('J3:J5000', CellIsRule(operator='lessThan', formula=['0'],fill=redFill))ListView一起使用来显示传入和传出消息。现在的问题是,当我单击用户可以在其中写消息的Entry(Textbox)时,键盘将打开,并且ListView在最后一条消息和Entry之间保留空间。

我读过很少的博客和聊天示例,但都没有一致的行为。

代码:

DataTemplate

方法1:MessagePage的KeyboardRendering:在ListView最后一条消息和SendTextBox之间保留空间

<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"
                ItemSelected="MyListView_OnItemSelected"
                ItemTapped="MyListView_OnItemTapped"
                IsPullToRefreshEnabled="true"
                IsRefreshing="{Binding IsRefreshing}"
                RefreshCommand="{Binding RefreshCommand}"
                SeparatorVisibility="None"
                BackgroundColor="{DynamicResource d8GreyLight}"
                //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"/>
        <Frame x:Name="SendButton">
          <Frame.GestureRecognizers>
            <TapGestureRecognizer Tapped="SendMessage_Click" NumberOfTapsRequired="1" />
          </Frame.GestureRecognizers>
          <Label Text="Send"/>
        </Frame>
      </Grid>
    </StackLayout>

</ContentPage.Content>

结果

enter image description here

方法2:MessagePage的KeyboardRendering:键盘向上和向下时SendTextBox的设计问题

using Project;
using Project.iOS;
using CoreGraphics;
using Foundation;
using System;
using UIKit;
using Xamarin.Forms;


[assembly: ExportRenderer(typeof(MessagePage), typeof(KeyboardAdaptedPageRenderer))]
namespace Project.iOS
{
    public class KeyboardAdaptedPageRenderer : ContentPageWithCustomBackButtonRenderer
    {
        //Way-1: Keep Space between ListView last message and SendTextBox
        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)
        {
            NSThread.SleepFor(1);
            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)
        {
            NSThread.SleepFor(1);

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

结果: enter image description here

0 个答案:

没有答案