Platform specific on iOS for setting CanBecomeFirstResponder (#8740) fixes #2136
authorShane Neuville <shneuvil@microsoft.com>
Fri, 6 Dec 2019 19:32:02 +0000 (12:32 -0700)
committerRui Marinho <me@ruimarinho.net>
Fri, 6 Dec 2019 19:32:02 +0000 (19:32 +0000)
* add ability for visual elements to become first responder

* cleanup page renderer

* added sample code

* Revert "cleanup page renderer"

This reverts commit 7429fd1debf68776a167aef117912c7a38d412e9.

* editor button becomes first responder

* remove extra declaration

* add mobile tag

* - add instructions to UI Test

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla45723.cs [new file with mode: 0644]
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
Xamarin.Forms.Core/PlatformConfiguration/iOSSpecific/VisualElement.cs
Xamarin.Forms.Platform.iOS/VisualElementRenderer.cs

diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla45723.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla45723.cs
new file mode 100644 (file)
index 0000000..f5d9825
--- /dev/null
@@ -0,0 +1,47 @@
+using Xamarin.Forms.CustomAttributes;
+using Xamarin.Forms.Internals;
+using Xamarin.Forms.PlatformConfiguration;
+using Xamarin.Forms.PlatformConfiguration.iOSSpecific;
+
+#if UITEST
+using Xamarin.UITest;
+using NUnit.Framework;
+#endif
+
+namespace Xamarin.Forms.Controls.Issues
+{
+       [Preserve(AllMembers = true)]
+       [Issue(IssueTracker.Bugzilla, 45723, "Entry / Editor and a Button. Tapping the button dismisses the keyboard", PlatformAffected.iOS)]
+       public class Bugzilla45723 : TestNavigationPage // or TestMasterDetailPage, etc ...
+       {
+               protected override void Init()
+               {
+                       PushAsync(new EditorAndButtonReproPage());
+               }
+       }
+
+       public class EditorAndButtonReproPage : ContentPage
+       {
+               public EditorAndButtonReproPage()
+               {
+                       BackgroundColor = Color.Gray;
+                       Padding = 50;
+                       var editor = new Editor { HorizontalOptions = LayoutOptions.FillAndExpand };
+                       var editorButton = new Button { Text = "OK", HorizontalOptions = LayoutOptions.End };
+                       editorButton.On<iOS>().SetCanBecomeFirstResponder(true);
+                       var editorLayout = new StackLayout { Orientation = StackOrientation.Horizontal, Children = { editor, editorButton }, VerticalOptions = LayoutOptions.Start };
+                       var endtry = new Entry { Placeholder = "Entry", HorizontalOptions = LayoutOptions.FillAndExpand };
+                       var entryButton = new Button { Text = "OK", HorizontalOptions = LayoutOptions.End };
+                       var entryLayout = new StackLayout { Orientation = StackOrientation.Horizontal, Children = { endtry, entryButton }, VerticalOptions = LayoutOptions.Start };
+                       Content = new StackLayout
+                       {
+                               Children = {
+                                       new Label() { Text = "Click editor to make keyboard appear. Click ok and keyboard shouldn't disappear"},
+                                       editorLayout,
+                                       new Label() { Text = "Click entry to make keyboard appear. Click ok and keyboard should disappear"},
+                                       entryLayout
+                               }
+                       };
+               }
+       }
+}
\ No newline at end of file
index b46857f..a28accb 100644 (file)
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla44338.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla44980.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla45067.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Bugzilla45723.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla45027.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla45330.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla44955.cs" />
index b3f6fc5..1fb3ded 100644 (file)
@@ -6,9 +6,7 @@ namespace Xamarin.Forms.PlatformConfiguration.iOSSpecific
 
        public static class VisualElement
        {
-               public static readonly BindableProperty BlurEffectProperty =
-                       BindableProperty.Create("BlurEffect", typeof(BlurEffectStyle),
-                       typeof(VisualElement), BlurEffectStyle.None);
+               public static readonly BindableProperty BlurEffectProperty = BindableProperty.Create("BlurEffect", typeof(BlurEffectStyle), typeof(VisualElement), BlurEffectStyle.None);
 
                public static BlurEffectStyle GetBlurEffect(BindableObject element)
                {
@@ -215,5 +213,28 @@ namespace Xamarin.Forms.PlatformConfiguration.iOSSpecific
                }
 
                #endregion
+               
+               public static readonly BindableProperty CanBecomeFirstResponderProperty = BindableProperty.Create(nameof(CanBecomeFirstResponder), typeof(bool), typeof(VisualElement), false);
+
+               public static bool GetCanBecomeFirstResponder(BindableObject element)
+               {
+                       return (bool)element.GetValue(CanBecomeFirstResponderProperty);
+               }
+
+               public static void SetCanBecomeFirstResponder(BindableObject element, bool value)
+               {
+                       element.SetValue(CanBecomeFirstResponderProperty, value);
+               }
+
+               public static bool CanBecomeFirstResponder(this IPlatformElementConfiguration<iOS, FormsElement> config)
+               {
+                       return GetCanBecomeFirstResponder(config.Element);
+               }
+
+               public static IPlatformElementConfiguration<iOS, FormsElement> SetCanBecomeFirstResponder(this IPlatformElementConfiguration<iOS, FormsElement> config, bool value)
+               {
+                       SetCanBecomeFirstResponder(config.Element, value);
+                       return config;
+               }
        }
 }
index 2cebe20..071077a 100644 (file)
@@ -109,6 +109,19 @@ namespace Xamarin.Forms.Platform.MacOS
                        platformEffect.SetControl(control);
                }
 
+#if __MOBILE__
+               public override bool CanBecomeFirstResponder
+               {
+                       get
+                       {
+                               if (Element.IsSet(PlatformConfiguration.iOSSpecific.VisualElement.CanBecomeFirstResponderProperty))
+                                       return PlatformConfiguration.iOSSpecific.VisualElement.GetCanBecomeFirstResponder(Element);
+
+                               return base.CanBecomeFirstResponder;
+                       }
+               }
+#endif
+
                void IEffectControlProvider.RegisterEffect(Effect effect)
                {
                        var platformEffect = effect as PlatformEffect;