我看到一个奇怪的行为,或者我可能完全错误的做法。
在底部有一个带有编辑器的页面,其中包含一个按钮和一个列表视图,顶部有一些消息。
我在这个链接中使用keyboardRenderer
https://xamgirl.com/adjusting-elements-when-keyboard-shows-in-xamarin-forms/
问题
当我开始键入时,键盘出现,编辑器完全崩溃并开始打字,它让我打字,但它崩溃了(见图片)也有时崩溃,看不到任何东西。
如何在iOS中使用带键盘的编辑器
倒塌
使用渲染器
[assembly: ExportRenderer(typeof(KeyboardView),
typeof(KeyboardViewRenderer))]
namespace KeyboardSample.iOS.Renderers
{
public class KeyboardViewRenderer : ViewRenderer
{
NSObject keyboardShowObserver;
NSObject keyboardHideObserver;
protected override void OnElementChanged(ElementChangedEventArgs<View> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
RegisterForKeyboardNotifications();
}
if (e.OldElement != null)
{
UnregisterForKeyboardNotifications();
}
}
private void RegisterForKeyboardNotifications()
{
if (keyboardShowObserver == null)
keyboardShowObserver = UIKeyboard.Notifications.ObserveWillShow(OnKeyboardShow);
if (keyboardHideObserver == null)
keyboardHideObserver = UIKeyboard.Notifications.ObserveWillHide(OnKeyboardHide);
}
private void OnKeyboardShow(object sender, UIKeyboardEventArgs args)
{
NSValue result = (NSValue)args.Notification.UserInfo.ObjectForKey(new NSString(UIKeyboard.FrameEndUserInfoKey));
CGSize keyboardSize = result.RectangleFValue.Size;
if (Element != null)
{
Element.Margin = new Thickness(0, 0, 0, keyboardSize.Height); //push the entry up to keyboard height when keyboard is activated
}
}
private void OnKeyboardHide(object sender, UIKeyboardEventArgs args)
{
if (Element != null)
{
Element.Margin = new Thickness(0); //set the margins to zero when keyboard is dismissed
}
}
private void UnregisterForKeyboardNotifications()
{
if (keyboardShowObserver != null)
{
keyboardShowObserver.Dispose();
keyboardShowObserver = null;
}
if (keyboardHideObserver != null)
{
keyboardHideObserver.Dispose();
keyboardHideObserver = null;
}
}
}
public class KeyboardView: Grid
{
}
在我的页面
<controls:KeyboardView Padding="0,60,0,0"
VerticalOptions="FillAndExpand">
<Grid RowSpacing="0" ColumnSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ListView Grid.Row="0">
<!--TODO -->
</ListView>
<Grid Grid.Row="1" RowSpacing="0" ColumnSpacing="0">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<StackLayout Orientation="Horizontal">
<ScrollView HorizontalOptions="FillAndExpand" >
<Editor
AutoSize="TextChanges"
Text="{Binding MyText, Mode=TwoWay}"
Margin="10"
TextColor="Black"
Keyboard="Chat"
BackgroundColor="Gainsboro"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
PlaceholderColor="LightGray"
Placeholder="Type your message here" />
</ScrollView>
<StackLayout HorizontalOptions="End" VerticalOptions="End">
<Button Text="Send" Command="{Binding TODOCommand}" Margin="0,10,10,10"/>
</StackLayout>
</StackLayout>
</Grid>
</Grid>
</controls:KeyboardView>
删除scrollview排序问题