gpt4 book ai didi

xamarin.ios - 键盘打开时的 Xamarin 按钮命令

转载 作者:行者123 更新时间:2023-12-05 07:42:08 26 4
gpt4 key购买 nike

我正在开发一个专门针对 iOS 平台的 Xamarin.Forms 项目。我有一个 Editor 控件和一个 Button 控件,彼此相邻。当我聚焦编辑器时,输入一些文本,然后单击按钮,看起来命令没有被触发,而是键盘只是关闭了。然后我必须再次点击添加按钮才能触发命令。

<StackLayout Orientation="Horizontal">
<Editor HorizontalOptions="FillAndExpand"
Text="{Binding EditorText}"/>
<Button Text="Add"
Command="{Binding AddCommand}"/>
</StackLayout>

我曾尝试创建一个自定义渲染器,以防止键盘最初关闭,然后在延迟后关闭它。这允许命令被触发,但我坚持打开键盘。

public class KeyboardEditorRenderer : EditorRenderer
{
protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == VisualElement.IsFocusedProperty.PropertyName)
{
if (Control != null)
{
Control.ShouldEndEditing = (UITextView textField) =>
{
Task.Delay(10).ContinueWith(_ =>
{
// THIS DOES NOT WORK
textField.EndEditing(true);
});

return false;
};
}
}

base.OnElementPropertyChanged(sender, e);
}
}

我理想的解决方案是您可以输入文本,点击添加按钮,键盘关闭,命令同时执行。关于如何实现这一目标的任何想法?

编辑:事实证明问题出在我用于页面的自定义渲染器上。自定义呈现器会在键盘出现时调整页面大小,使其不会覆盖我的编辑器字段。

public class KeyboardPageRenderer : PageRenderer
{
private bool keyboardShowing;
private NSObject keyboardWillShow;
private NSObject keyboardWillHide;
private double duration;
private UIViewAnimationCurve curve;

public override void ViewDidLoad()
{
base.ViewDidLoad();

this.keyboardWillShow = UIKeyboard.Notifications.ObserveWillShow(this.KeyboardWillShow);
this.keyboardWillHide = UIKeyboard.Notifications.ObserveWillHide(this.KeyboardWillHide);
}

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

this.keyboardWillShow.Dispose();
this.keyboardWillHide.Dispose();
}

private void KeyboardWillShow(object sender, UIKeyboardEventArgs args)
{
if (!this.keyboardShowing)
{
this.keyboardShowing = true;

var keyboardFrame = UIKeyboard.FrameBeginFromNotification(args.Notification);

this.duration = args.AnimationDuration;
this.curve = args.AnimationCurve;

this.ScrollTheView(true, keyboardFrame.Height);
}
}

private void KeyboardWillHide(object sender, UIKeyboardEventArgs args)
{
if (this.keyboardShowing)
{
this.keyboardShowing = false;

var keyboardFrame = UIKeyboard.FrameBeginFromNotification(args.Notification);

this.duration = args.AnimationDuration;
this.curve = args.AnimationCurve;

this.ScrollTheView(false, keyboardFrame.Height);
}
}

private void ScrollTheView(bool scale, nfloat scrollAmount)
{
UIView.BeginAnimations(string.Empty, IntPtr.Zero);
UIView.SetAnimationDuration(this.duration);
UIView.SetAnimationCurve(this.curve);

var frame = View.Frame;

// Assumes the page belongs to a tabbed view.
// This does not scale to pages that do not have one.
UITabBarController tabBarController = new UITabBarController();
nfloat tabHeight = tabBarController.TabBar.Frame.Size.Height;

scrollAmount -= tabHeight;

if (scale)
{
frame.Y -= scrollAmount;
}
else
{
frame.Y += scrollAmount;
}

View.Frame = frame;
UIView.CommitAnimations();
}
}

最佳答案

你的方法有两个问题

  • Task.Delay(10) 之后,您不再处于 UI 线程中,这意味着您必须使用 Device.BeginInvokeOnMainThread 才能访问 UI 元素。
  • Control.ShouldEndEditing 在调用 EndEditing
  • 之前必须清除

可行的解决方案如下所示:

protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);

if (Element == null || Control == null)
return;

VisualElement element = Element as VisualElement;
if (element == null)
return;

if (e.PropertyName == VisualElement.IsFocusedProperty.PropertyName && element.IsFocused == false)
{
Control.ShouldEndEditing = (UITextView control) =>
{
Device.BeginInvokeOnMainThread(() =>
{
control.ShouldEndEditing = null;
control.EndEditing(true);
});

// prevent the keyboard from closing
return false;
};
}
}

关于xamarin.ios - 键盘打开时的 Xamarin 按钮命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44791511/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com