Xamarin.forms Xamarin窗体显示提示应用键盘渲染器

Xamarin.forms Xamarin窗体显示提示应用键盘渲染器,xamarin.forms,Xamarin.forms,我的目标是在iOS上添加一个OK按钮;我可以通过自定义渲染器轻松实现这一点: public class ExtendedEntryRenderer : EntryRenderer { protected override void OnElementChanged(ElementChangedEventArgs<Entry> e) { base.OnElementChanged(e); if (Element == null)

我的目标是在iOS上添加一个OK按钮;我可以通过自定义渲染器轻松实现这一点:

public class ExtendedEntryRenderer : EntryRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (Element == null)
        {
            return;
        }

        // Check only for Numeric keyboard
        if (this.Element.Keyboard == Keyboard.Numeric)
        {
            this.AddDoneButton();
        }
    }

    /// <summary>
    /// <para>Add toolbar with Done button</para>
    /// </summary>
    protected void AddDoneButton()
    {
        var toolbar = new UIToolbar(new RectangleF(0.0f, 0.0f, 50.0f, 44.0f));

        var doneButton = new UIBarButtonItem(UIBarButtonSystemItem.Done, delegate
        {
            this.Control.ResignFirstResponder();
            var baseEntry = this.Element.GetType();
            ((IEntryController)Element).SendCompleted();
        });

        toolbar.Items = new UIBarButtonItem[] {
            new UIBarButtonItem (UIBarButtonSystemItem.FlexibleSpace),
            doneButton
        };
        this.Control.InputAccessoryView = toolbar;
    }
}

如果您想在iOS中自定义AlertView的键盘,可以使用DependencyService

以形式 创建一个接口

public interface IDisplayPrompt
{
   void DisplayPrompt(string Title,string Content,Keyboard keyboard,Action<string> SubmitAction,Action CancelAction);
}
公共接口IDisplayPrompt
{
无效显示提示(字符串标题、字符串内容、键盘、动作提交、动作取消动作);
}
在iOS中
使用系统;
使用app55;
使用app55.iOS;
使用Xamarin.Forms;
使用UIKit;
使用系统图;
[程序集:依赖项(typeof(DisplayPromptImplement))]
名称空间app55.iOS
{
公共类DisplayPrompImplement:IDisplayPrompt
{
公共显示提示实现()
{
}
公共无效显示提示(字符串标题、字符串内容、键盘、操作提交、操作取消操作)
{
UIAlertController=UIAlertController.Create(标题、内容、UIAlertControllerStyle.Alert);
UIAlertAction OKAction=UIAlertAction.Create(“确定”,UIAlertActionStyle.Default,(操作)=>{
//单击“确定”按钮
var content=alertController.TextFields[0].Text;
SubmitAction.Invoke(内容);
});
UIAlertAction DismissAction=UIAlertAction.Create(“取消”,UIAlertActionStyle.Cancel,(操作)=>{
//单击“取消”按钮
CancelAction.Invoke();
});
alertController.AddTextField((字段)=>{
如果(键盘==键盘.数字)
field.KeyboardType=UIKeyboardType.NumberPad;
AddDoneButton(字段);
});
alertController.AddAction(OKAction);
alertController.AddAction(DismissAction);
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(alertController,true,null);
}
受保护的void AddDoneButton(UITextField)
{
变量工具栏=新的UIToolbar(新的矩形F(0.0f、0.0f、50.0f、44.0f));
var doneButton=新UIBarButtonItem(UIBarButtonSystemItem.Done,委托
{
field.ResignFirstResponder();
});
toolbar.Items=newuibarbuttonItem[]{
新UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace),
顿布顿
};
field.InputAccessoryView=工具栏;
}
}
}
现在在表单中,我们可以像下面这样调用它

   void Button_Clicked(System.Object sender, System.EventArgs e)
    {
        if(Device.RuntimePlatform=="iOS")
        {
            DependencyService.Get<IDisplayPrompt>().DisplayPrompt("Title", "Please Input Message", Keyboard.Numeric, (content) =>
            {
                /// get the content that you input
                label.Text = content.ToString();

            }, null);
        }

        else
        {
            // other platform
            //...await DisplayPromptAsync
        }
    }
void按钮\u已单击(System.Object sender,System.EventArgs e)
{
if(Device.RuntimePlatform==“iOS”)
{
DependencyService.Get().DisplayPrompt(“标题”,“请输入消息”,键盘。数字,(内容)=>
{
///获取您输入的内容
label.Text=content.ToString();
},空);
}
其他的
{
//其他平台
//…等待DisplayPromptAsync
}
}

using System;
using app55;
using app55.iOS;
using Xamarin.Forms;
using UIKit;
using System.Drawing;

[assembly: Dependency(typeof(DisplayPromptImplement))]
namespace app55.iOS
{
    public class DisplayPromptImplement:IDisplayPrompt
    {
        public DisplayPromptImplement()
        {
        }

      

        public void DisplayPrompt(string Title, string Content, Keyboard keyboard, Action<string> SubmitAction, Action CancelAction)
        {
            UIAlertController alertController = UIAlertController.Create(Title,Content,UIAlertControllerStyle.Alert);

            UIAlertAction OKAction = UIAlertAction.Create("OK",UIAlertActionStyle.Default,(action)=> {

                //click OK Button

                var content = alertController.TextFields[0].Text;

                SubmitAction.Invoke(content);
            });

            UIAlertAction DismissAction = UIAlertAction.Create("Cancel", UIAlertActionStyle.Cancel, (action) => {

                //click Cancel Button

                CancelAction.Invoke();
            });

            alertController.AddTextField((field)=> {

                if (keyboard == Keyboard.Numeric)

                    field.KeyboardType = UIKeyboardType.NumberPad;
                    AddDoneButton(field);
                });

            alertController.AddAction(OKAction);
            alertController.AddAction(DismissAction);

            UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(alertController,true,null);
        }


        protected void AddDoneButton(UITextField field)
        {
            var toolbar = new UIToolbar(new RectangleF(0.0f, 0.0f, 50.0f, 44.0f));

            var doneButton = new UIBarButtonItem(UIBarButtonSystemItem.Done, delegate
            {
                field.ResignFirstResponder();
                              
            });

            toolbar.Items = new UIBarButtonItem[] {
            new UIBarButtonItem (UIBarButtonSystemItem.FlexibleSpace),
            doneButton
        };
            field.InputAccessoryView = toolbar;
        }
    }
}

   void Button_Clicked(System.Object sender, System.EventArgs e)
    {
        if(Device.RuntimePlatform=="iOS")
        {
            DependencyService.Get<IDisplayPrompt>().DisplayPrompt("Title", "Please Input Message", Keyboard.Numeric, (content) =>
            {
                /// get the content that you input
                label.Text = content.ToString();

            }, null);
        }

        else
        {
            // other platform
            //...await DisplayPromptAsync
        }
    }