iOS如何控制键盘下载事件

How to control the keyboard download event in iOS

我在 Xamarin 中有一个表单,其中有两个 <Entry>(输入控件),当执行 Tap 时,它会打开一个包含列表的模态 window,另一个具有正常操作,一切正常,直到用户首先打开普通的 Entry 键盘,然后调用模态,当发生这种情况时,键盘启动并且列表显示如下阻止用户的视图...

我想改变这个行为,所以附上视图的XAML代码

MyView.XAML:

      <Entry             
            Placeholder="Nombre Sustancia Química"
            Margin="15,5,15,5"
            HorizontalOptions="FillAndExpand"
            Text="{Binding NombreSustancia, Mode=TwoWay}"
            IsEnabled="{Binding EntryEnabled}">
        </Entry>


       <Entry    
            x:Name="Make"
            Placeholder="Seleccione Fabricante"
            Margin="15,5,15,5"
            Focused="Entry_Focused"
            HorizontalOptions="FillAndExpand"
            Text="{Binding NombreFabricante, Mode=TwoWay}"
            IsEnabled="{Binding EntryEnabled}">
       </Entry>           

MyView.XAML.CS:

public partial class FiltrosSisquimView : ContentPage
    {
        public ObservableCollection<Fabricante> Fabricantes { get; set; }

        public FiltrosSisquimView ()
        {              
            InitializeComponent();            
        }

        private async void Entry_Focused(object sender, FocusEventArgs e)
        {
            //prevents the keyboard from opening when calling the modal
            Make.Unfocus();          
            var mainViewModel = MainViewModel.GetInstance();
            Fabricantes = mainViewModel.Filtros.Fabricantes;
            mainViewModel.FabricantesModal = new FabricantesModalViewModel(Fabricantes);
            await Application.Current.MainPage.Navigation.PushModalAsync(new FabricantesModalView());
        }      

    }

我必须说,这个问题只发生在 iOS 中,并且 Android 具有预期的行为(打开模态时键盘会自动降低),键盘怎么会下降当用户已经打开列表时?我应该在哪里控制这个事件?在模式页面上?在代码隐藏?我把MVVM作为架构模式

对我有什么帮助吗?

似乎是原生 iOS 设计 issue.I 在 Xcode 上使用类似的代码并获得相同的 issue.So 您可以使用 custom renderer 并解决它。

in Forms

using System;
using Xamarin.Forms;
namespace app1
{
 public class MyEntry:Entry
 {
    public MyEntry()
    {

    }
 }
}

在 xaml

<Entry      
    Placeholder="Nombre Sustancia Química"
    Margin="15,5,15,5"
    HorizontalOptions="FillAndExpand"
       >
</Entry>


<local:MyEntry    
     x:Name="Make"
     Placeholder="Seleccione Fabricante"
     Margin="15,5,15,5" 
     Focused="Entry_Focused"      
     HorizontalOptions="FillAndExpand"
      >
</local:MyEntry>

in iOS project

using System;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

using app1;
using app1.iOS;

using UIKit;
using Foundation;


[assembly: ExportRenderer(typeof(MyEntry), typeof(MyEntryRenderer))]
namespace app1.iOS
{
  public class MyEntryRenderer:EntryRenderer,IUITextFieldDelegate
  {
    public MyEntryRenderer()
    {
    }

    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if(Control!=null)
        {
            Control.WeakDelegate = this;                       
        }

    }

    [Export("textFieldShouldBeginEditing:")]
    public bool ShouldBeginEditing(UITextField textField)
    {

        MessagingCenter.Send<Object>(this, "finish");

        return false;
    }

  }
}

现在,在您的 contentPage 中订阅消息。

public partial class FiltrosSisquimView : ContentPage
{
    public ObservableCollection<Fabricante> Fabricantes { get; set; }

    public FiltrosSisquimView ()
    {              
        InitializeComponent();    

        MessagingCenter.Subscribe<Object>(this, "finish", (obj) => {
        var mainViewModel = MainViewModel.GetInstance();
        Fabricantes = mainViewModel.Filtros.Fabricantes;
        mainViewModel.FabricantesModal = new FabricantesModalViewModel(Fabricantes);
        await Application.Current.MainPage.Navigation.PushModalAsync(new FabricantesModalView());
        });


    }

    private async void Entry_Focused(object sender, FocusEventArgs e)
    {
        //prevents the keyboard from opening when calling the modal
        Make.Unfocus();          
        var mainViewModel = MainViewModel.GetInstance();
        Fabricantes = mainViewModel.Filtros.Fabricantes;
        mainViewModel.FabricantesModal = new FabricantesModalViewModel(Fabricantes);
        await Application.Current.MainPage.Navigation.PushModalAsync(new FabricantesModalView());
    }      

}