简体   繁体   English

Xamarin Forms Entry调用Completed事件

[英]Xamarin Forms Entry invoke Completed event

I'm currently working on a login and registration page within Xamarin Forms and after changing the done button of the keyboard to next and go on the last one I am no longer receiving the Completed event on Android (working fine on iOS). 我目前正在Xamarin Forms中的登录和注册页面上工作,在将键盘的完成按钮更改为下一个并继续上一个后,我不再在Android上接收完成事件(在iOS上正常工作)。 In the custom renderer I can catch the Control.EditorAction event which now acts the same as the Completed event but I can't seem to invoke the Completed event on the entry itself. 在自定义渲染器中,我可以捕获Control.EditorAction事件,该事件现在与Completed事件的行为相同,但我似乎无法在条目本身上调用Completed事件。

Within the EntryRenderer 在EntryRenderer中

Control.EditorAction += (object sender, TextView.EditorActionEventArgsargs) =>
{
    if (entryExt.ReturnKeyType != ReturnKeyTypes.Next)
        entryExt.Unfocus();

    // Call all the methods attached to base_entry event handler Completed
    entryExt.InvokeCompleted();
};

And within the EntryExt (which extends the Entry directly) 在EntryExt中(直接扩展Entry)

public void InvokeCompleted()
{
    Completed?.Invoke(this, null);
}

But the Completed event cannot be invoked due to the error 但由于错误,无法调用Completed事件

Error CS0070: The event `Xamarin.Forms.Entry.Completed' can only appear on the left hand side of += or -= when used outside of the type `Xamarin.Forms.Entry'

Is there a way to invoke the Completed event? 有没有办法调用Completed事件? I'd rather not have a separate event handler for this within my views. 我宁愿在我的视图中没有单独的事件处理程序。

Fixed the issue by changing 通过更改修复了问题

entryExt.InvokeCompleted(); 

inside the EditorAction to 在EditorAction里面

((IEntryController)Element).SendCompleted(); 

which sends out the completed event back to the Entry base class. 它将完成的事件发送回Entry基类。

I've implemented a similar method of implementing this that allows the Return button to be extended to implement any type: Go, Next, Done, Send and Search. 我已经实现了一种类似的实现方法,允许将“ Return按钮扩展为实现任何类型:Go,Next,Done,Send和Search。

Sample App 示例应用程序

Here is a Xamarin.Forms app where I've implemented this approach. 这是一个Xamarin.Forms应用程序 ,我已经实现了这种方法。 Feel free to download the repo and try it out! 随意下载回购并试用吧!

1. Create Custom Entry 1.创建自定义条目

In the Xamarin.Forms PCL, create a Custom Entry 在Xamarin.Forms PCL中,创建自定义条目

public class EntryWithCustomKeyboardReturnButton : Entry
{
    public new event EventHandler Completed;

    public static readonly BindableProperty ReturnTypeProperty =
        BindableProperty.Create<EntryWithCustomKeyboardReturnButton, ReturnType>(s => s.ReturnType, ReturnType.Done);

    public ReturnType ReturnType
    {
        get { return (ReturnType)GetValue(ReturnTypeProperty); }
        set { SetValue(ReturnTypeProperty, value); }
    }

    public void InvokeCompleted()
    {
        Completed?.Invoke(this, null);
    }
}

public enum ReturnType
{
    Go,
    Next,
    Done,
    Send,
    Search
}

2. Create iOS Custom Renderer 2.创建iOS自定义渲染器

In the iOS PCL, create this custom renderer 在iOS PCL中,创建此自定义渲染器

[assembly: ExportRenderer(typeof(EntryWithCustomKeyboardReturnButton), typeof(EntryWithCustomKeyboardReturnButtonCustomRenderer))]
namespace Sample.iOS
{
    public class EntryWithCustomKeyboardReturnButtonCustomRenderer : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            var customEntry = Element as EntryWithCustomKeyboardReturnButton;

            if (Control != null && customEntry != null)
            {
                SetKeyboardButtonType(customEntry.ReturnType);

                Control.ShouldReturn += (UITextField tf) =>
                {
                    customEntry?.InvokeCompleted();
                    return true;
                };
            }
        }

        void SetKeyboardButtonType(ReturnType returnType)
        {
            switch (returnType)
            {
                case ReturnType.Go:
                    Control.ReturnKeyType = UIReturnKeyType.Go;
                    break;
                case ReturnType.Next:
                    Control.ReturnKeyType = UIReturnKeyType.Next;
                    break;
                case ReturnType.Send:
                    Control.ReturnKeyType = UIReturnKeyType.Send;
                    break;
                case ReturnType.Search:
                    Control.ReturnKeyType = UIReturnKeyType.Search;
                    break;
                case ReturnType.Done:
                    Control.ReturnKeyType = UIReturnKeyType.Done;
                    break;
                default:
                    Control.ReturnKeyType = UIReturnKeyType.Default;
                    break;
            }
        }
    }
}

3. Create Android Custom Renderer 3.创建Android自定义渲染器

In the Android PCL, create this custom renderer 在Android PCL中,创建此自定义渲染器

[assembly: ExportRenderer(typeof(EntryWithCustomKeyboardReturnButton), typeof(EntryWithCustomKeyboardReturnButtonCustomRenderer))]
namespace Sample.Droid
{
    public class EntryWithCustomKeyboardReturnButtonCustomRenderer : EntryRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            var customEntry = Element as EntryWithCustomKeyboardReturnButton;

            if (Control != null && customEntry != null)
            {
                SetKeyboardButtonType(customEntry.ReturnType);

                Control.EditorAction += (object sender, TextView.EditorActionEventArgs args) =>
                {
                    if (customEntry?.ReturnType != ReturnType.Next)
                        customEntry?.Unfocus();

                    customEntry?.InvokeCompleted();
                };
            }
        }

        void SetKeyboardButtonType(ReturnType returnType)
        {
            switch (returnType)
            {
                case ReturnType.Go:
                    Control.ImeOptions = ImeAction.Go;
                    Control.SetImeActionLabel("Go", ImeAction.Go);
                    break;
                case ReturnType.Next:
                    Control.ImeOptions = ImeAction.Next;
                    Control.SetImeActionLabel("Next", ImeAction.Next);
                    break;
                case ReturnType.Send:
                    Control.ImeOptions = ImeAction.Send;
                    Control.SetImeActionLabel("Send", ImeAction.Send);
                    break;
                case ReturnType.Search:
                    Control.ImeOptions = ImeAction.Search;
                    Control.SetImeActionLabel("Search", ImeAction.Search);
                    break;
                default:
                    Control.ImeOptions = ImeAction.Done;
                    Control.SetImeActionLabel("Done", ImeAction.Done);
                    break;
            }
        }
    }
}

4. Implement the Completed Event for Entry in Xamarin.Forms PCL 4.在Xamarin.Forms PCL中实现Entry的完成事件

Here is a link to some sample code that shows how to implement the Completed Event in the Xamarin.Forms PCL. 以下是一些示例代码链接,代码显示了如何在Xamarin.Forms PCL中实现Completed Event。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM