iOS 上 VisualElement 第一响应方

此项 iOS 平台特定功能可支持 VisualElement 对象而不是包含该元素的页面成为触摸事件的第一响应方。 在 XAML 中,可将 VisualElement.CanBecomeFirstResponder 绑定属性设置为 true 来使用:

<ContentPage ...
             xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core">
    <StackLayout>
        <Entry Placeholder="Enter text" />
        <Button ios:VisualElement.CanBecomeFirstResponder="True"
                Text="OK" />
    </StackLayout>
</ContentPage>

或者,可以使用 Fluent API 从 C# 使用它:

using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.iOSSpecific;
...

Entry entry = new Entry { Placeholder = "Enter text" };
Button button = new Button { Text = "OK" };
button.On<iOS>().SetCanBecomeFirstResponder(true);

VisualElement.On<iOS> 方法指定此平台特定仅在 iOS 上运行。 命名空间 Xamarin.Forms.PlatformConfiguration.iOSSpecificVisualElement.SetCanBecomeFirstResponder 的方法用于设置 VisualElement 以使其成为触摸事件的第一响应方。 此外,VisualElement.CanBecomeFirstResponder 方法可用于返回 VisualElement 是否为触摸事件的第一响应方。

结果是 VisualElement 而不是包含该元素的页面可以成为触摸事件的第一响应方。 通过此设置,聊天应用程序等方案能够在点击 Button 时不会关闭键盘。