Jaa


VisualElement first responder on iOS

This iOS platform-specific enables a VisualElement object to become the first responder to touch events, rather than the page containing the element. It's consumed in XAML by setting the VisualElement.CanBecomeFirstResponder bindable property to 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>

Alternatively, it can be consumed from C# using the fluent API:

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);

The VisualElement.On<iOS> method specifies that this platform-specific will only run on iOS. The VisualElement.SetCanBecomeFirstResponder method, in the Xamarin.Forms.PlatformConfiguration.iOSSpecific namespace, is used to set the VisualElement to become the first responder for touch events. In addition, the VisualElement.CanBecomeFirstResponder method can be used to return whether the VisualElement is the first responder to touch events.

The result is that a VisualElement can become the first responder for touch events, rather than the page containing the element. This enables scenarios such as chat applications not dismissing a keyboard when a Button is tapped.