【问题标题】:Scroll editor in Xamarin Forms into view将 Xamarin Forms 中的编辑器滚动到视图中
【发布时间】:2014-08-12 17:45:19
【问题描述】:

使用 Xamarin 表单,请考虑下面的 Xaml。

<StackLayout VerticalOptions="FillAndExpand">
   <Image x:Name="cameraImage" Source="camera.png" />
   <Label Text="Describe the image" />
   <Editor />
   <Button Text="Save" />
 </StackLayout>

这会渲染一个图像、一个编辑器和一个保存按钮。图像的图像比例为 4x3,覆盖了可用屏幕高度的大约三分之一。编辑器如下所示。

问题是键盘覆盖了 iOS 中的编辑器。通常是标准的iOS问题。

问题是:Xamarin Forms 的处理方式是什么?

谢谢

// 约翰

【问题讨论】:

    标签: ios xamarin.ios xamarin xamarin.forms


    【解决方案1】:

    我在编写一个小型聊天应用程序时偶然发现了这个问题,它基本上包含一个可滚动的消息列表、一个文本条目和一个发送按钮:

    之前发布的解决方案的问题是您需要嵌套两个滚动视图,Xamarin.Forms documentation 不建议这样做。为了防止键盘隐藏条目,我发现了以下 hack:

    我在主堆栈布局的末尾添加了一个placeholder。根据条目是否聚焦(即键盘是否可见),占位符的高度设置为 0 或键盘高度。

            // HACK: make entry visible when keyboard open
            var placeholder = new BoxView {
                HeightRequest = 0,
            };
            entry.Focused += (sender, e) => placeholder.HeightRequest = 210;
            entry.Unfocused += (sender, e) => placeholder.HeightRequest = 0;
    
            Content = new StackLayout {
                VerticalOptions = LayoutOptions.Fill,
                Padding = 5,
                Children = {
                    whoTable,
                    messageScrollView,
                    new StackLayout {
                        Orientation = StackOrientation.Horizontal,
                        VerticalOptions = LayoutOptions.End,
                        HeightRequest = 70,
                        Children = {
                            entry,
                            sendButton,
                        },
                    },
                    placeholder,
                },
            };
    

    当然,这并不完美。特别是硬编码的键盘高度应该更优雅地实现。也许你应该只在 iOS 上应用它,而不是在 Android 上。

    【讨论】:

      【解决方案2】:

      要使用 Xamarin.Forms 为编辑器和条目自动滚动,通常只需将视图(在本例中为 StackLayout)打包到 ScrollView:

      <ScrollView>
          <StackLayout VerticalOptions="FillAndExpand">
              <Image x:Name="cameraImage" Source="camera.png" />
              <Label Text="Describe the image" />
              <Editor />
              <Button Text="Save" />
          </StackLayout>
      </ScrollView>
      

      它应该是这样工作的,但截至今天(2014 年 6 月),有一个错误阻止它完全与编辑器一起工作(它适用于条目)。这个问题是已知的并且正在解决。

      [UPDATE 2014-11-20]该问题已得到解决,并将在 XF 1.3 的下一个预发布版本中提供

      【讨论】:

      • 如果你有一个列表视图,这不会很好。将滚动视图与列表视图混合会弄乱拖动事件
      • 就像@NavySeal 和 Falko 所写的那样——将滚动嵌入到滚动中并不是最佳实践(不仅在表单中)。这不适用于表情符号或第 3 方(如 SwiftKey)等非标准键盘
      【解决方案3】:

      有时您不能将主视图放在滚动视图中,在这种情况下,您可以通过处理 iOS 项目的键盘事件并将它们传递到表单级别来实现这一点。 Android 会照顾好自己。

      using System;
      using Foundation;
      using UIKit;
      using RaiseKeyboard.iOS;
      
      [assembly: Xamarin.Forms.Dependency (typeof (KeyboardHelper))]
      namespace RaiseKeyboard.iOS
      {
          // Raises keyboard changed events containing the keyboard height and
          // whether the keyboard is becoming visible or not
          public class KeyboardHelper : IKeyboardHelper
          {
              public KeyboardHelper() {
                  NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillHideNotification, OnKeyboardNotification);
                  NSNotificationCenter.DefaultCenter.AddObserver(UIKeyboard.WillShowNotification, OnKeyboardNotification);
              }
      
              public event EventHandler<KeyboardHelperEventArgs> KeyboardChanged;
      
              private void OnKeyboardNotification (NSNotification notification)
              {
                  var visible = notification.Name == UIKeyboard.WillShowNotification;
                  var keyboardFrame = visible
                      ? UIKeyboard.FrameEndFromNotification(notification)
                      : UIKeyboard.FrameBeginFromNotification(notification);
                  if (KeyboardChanged != null) {
                      KeyboardChanged (this, new KeyboardHelperEventArgs (visible, (float)keyboardFrame.Height));
                  }
              }
          }
      }
      

      然后在表单级别:

      using System;
      using Xamarin.Forms;
      
      namespace RaiseKeyboard
      {
          // Provides static access to keyboard events
          public static class KeyboardHelper
          {
              private static IKeyboardHelper keyboardHelper = null;
      
              public static void Init() {
                  if (keyboardHelper == null) {
                      keyboardHelper = DependencyService.Get<IKeyboardHelper>();
                  }
              }
      
              public static event EventHandler<KeyboardHelperEventArgs> KeyboardChanged {
                  add {
                      Init();
                      keyboardHelper.KeyboardChanged += value;
                  }
                  remove {
                      Init ();
                      keyboardHelper.KeyboardChanged -= value;
                  }
              }
          }
      
          public interface IKeyboardHelper
          {
              event EventHandler<KeyboardHelperEventArgs> KeyboardChanged;
          }
      
          public class KeyboardHelperEventArgs : EventArgs 
          {
              public readonly bool Visible;
              public readonly float Height;
      
              public KeyboardHelperEventArgs(bool visible, float height) {
                  Visible = visible;
                  Height = height;
              }
          }
      
      }
      

      如果您在 Stacklayout 中工作并希望将视图提升到键盘上方,您可以在堆栈底部放置一个高度为 0 的垫片。然后将其设置为键盘更改事件引发时的键盘高度。

      spacer.HeightRequest = e.Visible ? e.Height : 0;
      

      如果您正在使用 Listview,您可以通过将视图转换为重叠的数量来处理此问题。

      bottomOffset = mainStack.Bounds.Bottom - textStack.Bounds.Bottom;
      textStack.TranslationY -= e.Visible ? e.Height - bottomOffset : bottomOffset - e.Height;
      

      必须以不同的方式处理列表视图,因为高度会由 Forms 自动调整,并且使用间隔会导致过度校正。

      示例:https://github.com/naturalistic/raisekeyboard

      【讨论】:

      • 非常好的解决方案丹尼尔
      • 这是一个很好的解决方案。但是,它不考虑存在硬件键盘。 :-\
      【解决方案4】:

      扩展 @Falko 的答案,您可以检查 iOS 平台,因为 Android 会按预期本机处理此问题。

      我还通过this answer 将其添加到页面中,以实现快速而肮脏的定位。

      static bool IsPortrait(Page p) { return p.Width < p.Height; }
      

      无论如何,我知道 Xamarin 很快就会为此添加一些解决方案。不过现在……

      protected async void Message_Focused(object sender, EventArgs args)
      {
          if (Device.OS == TargetPlatform.iOS)
          {
              //TLR: still need a way to determine the iOS keyboard's height first
              //until then, this is a functional hack
      
              if (IsPortrait(this))
              {
                  KeyboardSpacer.HeightRequest = 165;
              }
              else
              {
                  KeyboardSpacer.HeightRequest = 114;
              }
          }
      }
      
      protected async void Message_Unfocused(object sender, EventArgs args)
      {            
          if (Device.OS == TargetPlatform.iOS)
          {
              KeyboardSpacer.HeightRequest = 0;
          }
      }
      

      【讨论】:

        【解决方案5】:

        注意,raisekeyboard 只是为应用程序中的一个条目实现的,如果您添加一个新条目,KeyboardHelper.KeyboardChanged 将在焦点位于任何条目时拍摄。

        KeyboardHelper.KeyboardChanged += (sender, e) =>{
            bottomOffset = this.ParentView.Bounds.Bottom - _editor.Bounds.Bottom;
            if (KeyboardStatus)
                _editor.TranslationY = e.Visible ? -(e.Height - bottomOffset) : 0;
            else
                _editor.TranslationY = 0;
        };
        

        【讨论】:

          【解决方案6】:

          使用上面提到的 ScrollView 修复了 iOS 中的问题并部分修复了 Android 中的问题。为了完全解决android中的问题,我发现了另一个简单而漂亮的补充。

          仅在 android 中,我将 Xamarin 表单编辑器替换为 android 特定的 TextEdit 控件。因此,在我的 Page 构造函数中,我有以下仅适用于 android 的代码。

          #if __ANDROID__
                   // I have editor defined in xaml named ReportTextEditor in stacklayout named MainStackLayout
                  Editor sharedEditor = this.ReportTextEditor;
                  MainStackLayout.Children.Remove(sharedEditor); //removing the ReportTextEditor which was defined in xaml
                  EditText editText = new EditText(Forms.Context); //created android specific editor
                  editText.InputType = InputTypes.ClassText | InputTypes.TextFlagAutoCorrect | InputTypes.TextFlagCapSentences | InputTypes.TextFlagMultiLine; //keyboard options, optional
                  MainStackLayout.Children.Add(editText); //Added android specific edit text to my stack layout
          
          #endif
          

          您还需要根据需要添加 android 特定的命名空间。

          #if __ANDROID__
          using Xamarin.Forms.Platform.Android;
          using Android.Widget;
          using Button = Xamarin.Forms.Button;
          using Android.Text;
          #endif
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-11-12
            • 2016-12-31
            • 1970-01-01
            • 2021-08-20
            相关资源
            最近更新 更多