4

是)我有的:

我有一个自定义类MyEntry派生自 Android 和 iOS 的Xamarin.Forms.Entry自定义渲染器类MyEntryRenderer

我想要的是:

ImeOptions我想通过打开AndroidReturnKeyType打开将键盘的“输入”按钮更改为“搜索”按钮iOS(参见示例代码)。当我按下更改后的“搜索”按钮时,MyEntry.Completed应该调用该事件(就像之前按下未更改的“输入”按钮时一样。

真正发生的事情:

代码按iOS预期工作。但是Android什么都没有发生。该事件不会被调用。

我的问题:

我怎样才能实现我上面描述的Android

示例代码:

应用程序.cs:

namespace CustomEntry
{
    public class App
    {
        public static Page GetMainPage()
        {    
            MyEntry entry = new MyEntry {
                VerticalOptions = LayoutOptions.CenterAndExpand,
                HorizontalOptions = LayoutOptions.CenterAndExpand,
                Placeholder = "Enter some text"
            };

            entry.Completed += delegate {
                Console.WriteLine("Completed");
            };

            return new ContentPage { 
                Content = entry,
            };
        }
    }
}

我的条目.cs:

namespace CustomEntry
{
    public class MyEntry:Entry
    {

    }
}

MyEntryRenderer.cs (Android):

[assembly: ExportRenderer(typeof(MyEntry), typeof(MyEntryRenderer))]
namespace CustomEntry.Android
{
    public class MyEntryRenderer:EntryRenderer
    {

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

            if (Control != null) { 
                Control.ImeOptions = global::Android.Views.InputMethods.ImeAction.Search;
            }
        }

    }
}

MyEntryRenderer.cs (iOS):

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

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

            if (Control != null) {
                Control.ReturnKeyType = UIReturnKeyType.Search;
            }
        }

    }
}
4

2 回答 2

4

我自己找到了解决问题的方法:

首先Action,当我按下“搜索”按钮时,我添加了一个要调用的自定义条目。

我的条目.cs

namespace CustomEntry
{
    public class MyEntry:Entry
    {
        public Action SearchPressed = delegate {
        };
    }
}

其次,我像这样“听”ImeAction.Search并调用Action我添加到我的自定义条目中的。

MyEntryRenderer.cs (Android):

[assembly: ExportRenderer(typeof(MyEntry), typeof(MyEntryRenderer))]
namespace CustomEntry.Android
{
    public class MyEntryRenderer:EntryRenderer
    {

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

            if (Control != null) {
                Control.ImeOptions = ImeAction.Search;
                Control.EditorAction += (sender, args) => {
                    if (args.ActionId == ImeAction.Search) {
                        var entry = (AddressEntry)Element;
                        entry.SearchPressed();
                    }
                };
            }
        }

    }
}

在我使用的第三类中,MyEntry当按下“搜索”按钮时,我可以运行一些代码,如下所示:

var myEntry = new MyEntry();
myEntry.SearchPressed += SomeMethod;
于 2014-12-31T09:20:43.460 回答
0

公共类 EntryExtensionRenderer : EntryRenderer { 受保护的覆盖无效 OnElementChanged(ElementChangedEventArgs e) { base.OnElementChanged(e);

        if ((Element as EntryExtension).NoSuggestionsKey)
        {
            Control.AutocorrectionType = UITextAutocorrectionType.No;
        }

        if ((Element as EntryExtension).ReturnKeyType.Equals("Done"))
        {
            this.AddDoneButton("Done", (EntryExtension)Element);
        }
        else if ((Element as EntryExtension).ReturnKeyType.Equals("Next"))
        {
            this.AddDoneButton("Next", (EntryExtension)Element);
        }
    }

    protected void AddDoneButton(string button, EntryExtension entry)
    {
        UIToolbar toolbar = new UIToolbar(new RectangleF(0.0f, 0.0f, 50.0f, 44.0f));

        var doneButton = new UIBarButtonItem();

        if (button.Equals("Done")) { 
            doneButton = new UIBarButtonItem(UIBarButtonSystemItem.Done, delegate
            {
                entry.KeyPressedEnter();
            });
        }

        if (button.Equals("Next"))
        {
            doneButton = new UIBarButtonItem("Next", UIBarButtonItemStyle.Done, delegate
            {
                entry.KeyPressedEnter();
            });
        }

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


}
于 2016-08-08T04:00:58.207 回答